带有 CURL 的 PHP 中的 SOAP 请求 [英] SOAP request in PHP with CURL

查看:29
本文介绍了带有 CURL 的 PHP 中的 SOAP 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 php.net 上的 SOAP 手册对新手不太友好,而且我找不到任何好的示例,因此我将在此处发布我的问题.

Since the SOAP manual on php.net is not very noob friendly and I could not find any good examples I will post my question here.

如何创建这样的 PHP SOAP 请求?

How can I create PHP SOAP request to look like this?

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 连接

非常感谢任何建议/链接/示例.

Any suggestion / links / example much appreciated.

推荐答案

测试有效!

  • 使用 https、用户和密码

  • with https, user & password

 <?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 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. 
 ?>

这篇关于带有 CURL 的 PHP 中的 SOAP 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆