我已經提供了一個.wsdl-URI和一個Java示例(見下文),需要通過SSL進行SOAP調用並處理響應。我使用PHP和Zend Framework,但無法弄清楚從哪裏開始。我早些時候使用C#.NET調用SOAP服務,但通過嚮導配置服務後,只需要2-3行代碼。有沒有簡單的方法來進行調用並處理結果XML?如果沒有,我可以在哪裏找到一個很好的例子讓我開始?使用Zend框架通過SSL進行SOAP調用
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPFault;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPHeader;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
public class RegistryDetails {
private static final long serialVersionUID= 1L;
private static final String SERVICE = "https://ServiceProvider.com/TheService.pl";
private static final String TAG_GETPRODUCT = "getDetails";
private static String namespaceService = "http://ServiceProvider.com/TheService.xsd";
private static String namespaceUserSession = "http://ServiceProvider.com/UserSession.xsd";
private static String SYSTEM = "thesystem";
private static String USERNAME = "theusername";
private static String PASSWORD = "thepassword";
public static void main(String[] args) {
try {
//Create the connection
SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = soapConnFactory.createConnection();
//Create the actual message
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
MimeHeaders mimeHeader = message.getMimeHeaders();
mimeHeader.addHeader("SOAPAction", "");
//Create objects for the message parts
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPHeader header = envelope.getHeader();
SOAPElement userSession = header.addChildElement("UserSession", "UserSession", namespaceUserSession);
userSession.addChildElement("TypeOfService").addTextNode("Integrated");
userSession.addChildElement("System").addTextNode(SYSTEM);
userSession.addChildElement("Username").addTextNode(USERNAME);
userSession.addChildElement("Password").addTextNode(PASSWORD);
SOAPBody body = envelope.getBody();
//Populate the body
//Create the main element and namespace
SOAPElement getDetails = body.addChildElement(TAG_GETPRODUCT, "dfg", namespaceService);
getDetails.addChildElement("queryField1").addTextNode("xxx");
getDetails.addChildElement("queryField2").addTextNode("yyy");
getDetails.addChildElement("queryField3").addTextNode("zzz");
//Save the message
message.saveChanges();
//Check the input
System.out.println("\nREQUEST:\n");
message.writeTo(System.out);
System.out.println();
//Send the message and get a reply
//Send the message
SOAPMessage reply = connection.call(message, SERVICE);
System.out.println("\nRESPONSE:\n");
reply.writeTo(System.out);
System.out.println();
{
SOAPBody retbody = reply.getSOAPBody();
if (retbody.hasFault()) {
SOAPFault fault = retbody.getFault();
System.out.println("SOAPfault: " + fault.getFaultString());
}
else {
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("*/RESULT/HOV/ReturnField1");
String returnField1 = (String)expr.evaluate(retbody, XPathConstants.STRING);
System.out.println("Result: " + returnField1);
}
}
//Close the connection
connection.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
你有什麼嘗試過,發佈你的嘗試和輸出的PHP代碼示例... – danielrsmith 2012-03-15 15:31:24
我試過什麼都沒有,因爲我不知道從哪裏開始。正如Tim所評論的那樣,有一些文檔是zend.com,但沒有任何內容適用於我正在做的事情。如何將命名參數發送到SOAP函數調用?如何添加不同的節點,例如Java示例呢?我會根據這個例子編寫一個簡單的3行事件來證明我已經嘗試過了,但我不確定這個討論有多大的價值。我的意思是...... Java的例子甚至沒有在任何地方使用WSDL文件。 – rymored 2012-03-15 22:27:05
'Zend_Soap_Client'擴展了通用PHP'SoapClient'看看它的文檔http://us3.php.net/manual/en/soapclient.soapclient.php – danielrsmith 2012-03-16 21:24:19