2012-03-15 32 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