使用CURL在PHP中发送SOAP请求

75

由于php.net上的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 - J0HN
SoapClient类 - hakre
1个回答

178

测试并且可用!

  • 使用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 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. 
 ?>

1
这个脚本运行良好,返回XML SOAP响应pastebin.com/9wzUV8Pw,但我无法将响应解析为字符串,有什么想法吗? - Irfan
没有测试,simplexml_load_string()无法处理“:”,对吗? - whereismydipp
我正在尝试使用curl消费一个SOAP webservice,但是我只收到一个错误,说“不支持的媒体类型”。有没有一种方法可以强制Content-type头?当我设置CURLOPT_POSTFIELDS时,Content-type会更改为“application/x-www-form-urlencoded”。 - Gustavo Straube
1
我在这里找到了问题!我正在使用关联数组编写标题。当阅读此答案时,我才注意到这一点:https://dev59.com/2mXWa4cB1Zd3GeqPJhzN#11091261 - Gustavo Straube
截至2017年2月,这仍然按预期工作,并在使用SOAP时帮助人们解决头痛问题。我只想指出一个事实,即一些服务器需要为xml标签添加前缀(例如<clin:price>),否则响应会包含有关未识别子元素的消息。 - Erenor Paz
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接