【发布时间】:2012-03-15 13:45:14
【问题描述】:
我已经获得了一个 .wsdl-URI 和一个 Java 示例(见下文),并且需要通过 SSL 进行 SOAP 调用并处理响应。我正在使用 PHP 和 Zend 框架,但不知道从哪里开始。我之前使用 C#.NET 调用了 SOAP 服务,但在通过向导配置服务后只需要 2-3 行代码。是否有一种简单的方法可以进行此调用并处理生成的 XML?如果没有,我在哪里可以找到一个很好的例子来帮助我入门?
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 代码示例和输出......
-
我什么都没试过,因为我不确定从哪里开始。正如蒂姆所评论的那样,有一些文档作为 zend.com,但没有适用于我正在做的事情。如何将命名参数发送到 SOAP 函数调用中?如何像 Java 示例一样添加不同的节点?我会根据示例编写一个简单的 3 行内容,以表明我已经尝试过,但我不确定这对这个讨论有多大价值。我的意思是.. Java 示例甚至没有在任何地方使用 WSDL 文件。
-
Zend_Soap_Client扩展了通用 PHPSoapClient看看它的文档 us3.php.net/manual/en/soapclient.soapclient.php -
啊,终于 - 这实际上回答了一些问题。它显示了如何发送 inn 多个命名参数,但我还有一个问题没有得到解答:该示例显示信息被分成两个不同的 XML 节点(UserSession 和 GetDetails)。 WSDL 文件/SOAP 函数是否自动处理这个问题,或者我应该以某种方式自己处理这个问题(即发送两个不同的 SOAP 请求或在同一个请求中调用两个函数或类似的东西)?
标签: php zend-framework soap ssl frameworks