2011-08-19 202 views
55

由於php.net上的SOAP手冊不是很友好,我找不到任何好的示例,所以我會在這裏發佈我的問題。使用CURL PHP的SOAP請求

如何創建PHP SOAP請求,看起來像這樣?

POST /MySERVER/myWSDLservice.asmx HTTP/1.1 
Host: connection.mywebsite.com 
Content-Type: text/xml; charset=utf-8 
Content-Length: length 
SOAPAction: "http://connection.mywebsite.com/MySERVER/GetCarType" 

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
<soap:Body> 
    <GetCarType xmlns="http://connection.mywebsite.com/MySERVER/"> 
    <IDNumber>string</IDNumber> 
    </GetCarType> 
</soap:Body> 
</soap:Envelope> 

請注意:

  • 沒有用戶名/密碼身份驗證
  • SSL連接

任何建議/鏈接/例如大加讚賞。

+1

[PHP :: SOAP](http://php.net/manual/en/book.soap.php) – J0HN

+0

[ SoapClient類](http://www.php.net/manual/en/class.soapclient.php) – hakre

回答

128

經過測試和工作!

  • 以https,用戶&密碼

     <?php 
         //Data, connection, auth 
         $dataFromTheForm = $_POST['fieldName']; // request data from the form 
         $soapUrl = "https://connecting.website.com/soap.asmx?op=DoSomething"; // asmx URL of WSDL 
         $soapUser = "username"; // username 
         $soapPassword = "password"; // password 
    
         // xml post structure 
    
         $xml_post_string = '<?xml version="1.0" encoding="utf-8"?> 
              <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
               <soap:Body> 
               <GetItemPrice xmlns="http://connecting.website.com/WSDL_Service"> // xmlns value to be set to your's WSDL URL 
                <PRICE>'.$dataFromTheForm.'</PRICE> 
               </GetItemPrice > 
               </soap:Body> 
              </soap:Envelope>'; // data from the form, e.g. some ID number 
    
          $headers = array(
             "Content-type: text/xml;charset=\"utf-8\"", 
             "Accept: text/xml", 
             "Cache-Control: no-cache", 
             "Pragma: no-cache", 
             "SOAPAction: http://connecting.website.com/WSDL_Service/GetPrice", 
             "Content-length: ".strlen($xml_post_string), 
            ); //SOAPAction: your op URL 
    
          $url = $soapUrl; 
    
          // PHP cURL for https connection with auth 
          $ch = curl_init(); 
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1); 
          curl_setopt($ch, CURLOPT_URL, $url); 
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
          curl_setopt($ch, CURLOPT_USERPWD, $soapUser.":".$soapPassword); // username and password - declared at the top of the doc 
          curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
          curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
          curl_setopt($ch, CURLOPT_POST, true); 
          curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request 
          curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    
          // converting 
          $response = curl_exec($ch); 
          curl_close($ch); 
    
          // converting 
          $response1 = str_replace("<soap:Body>","",$response); 
          $response2 = str_replace("</soap:Body>","",$response1); 
    
          // convertingc to XML 
          $parser = simplexml_load_string($response2); 
          // user $parser to get your data out of XML response and to display it. 
        ?> 
    
+0

這對我來說就像一個魅力。謝謝阿米特。 – wsams

+0

這個腳本對我來說真的很有幫助。它節省了我很多時間.. – Pank

+0

這個腳本很好用,並返回XML SOAP響應pastebin.com/9wzUV8Pw,但我無法將響應解析爲字符串,任何想法? –