2012-03-15 87 views
-1

我已经提供了一个.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()); 
     } 
    } 
} 
+0

你有什么尝试过,发布你的尝试和输出的PHP代码示例... – danielrsmith 2012-03-15 15:31:24

+0

我试过什么都没有,因为我不知道从哪里开始。正如Tim所评论的那样,有一些文档是zend.com,但没有任何内容适用于我正在做的事情。如何将命名参数发送到SOAP函数调用?如何添加不同的节点,例如Java示例呢?我会根据这个例子编写一个简单的3行事件来证明我已经尝试过了,但我不确定这个讨论有多大的价值。我的意思是...... Java的例子甚至没有在任何地方使用WSDL文件。 – rymored 2012-03-15 22:27:05

+0

'Zend_Soap_Client'扩展了通用PHP'SoapClient'看看它的文档http://us3.php.net/manual/en/soapclient.soapclient.php – danielrsmith 2012-03-16 21:24:19

回答

0

http://framework.zend.com/manual/en/zend.soap.client.html - 传入您的WSDL URL,然后您可以根据需要拨打电话。

+0

我已经通过zend.com上的文档没有找到任何我可以涉及到我想要完成的东西。我如何发送参数?我如何指定不同部分的信息(userSession和getDetails)? WSDL处理这些吗? – rymored 2012-03-15 14:15:43

+0

Java示例不是很清楚 - 您是否有任何其他Web服务本身的文档? – 2012-03-15 16:22:58

+0

我还有一些文档,但由于与服务提供商签订了保密协议,因此我无法重写内容。明天我会尽量做到这一点,但在此之前:有没有人有一个具体的例子来说明如何使用多个参数调用SOAP服务,并将一些返回的XML节点提取到PHP变量中?我认为,如果我只理解SOAP调用和响应处理的方式,我就可以开始测试,也许自己将其余部分弄清楚。 – rymored 2012-03-15 22:33:12