Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP中带CURL的SOAP请求_Php_Soap_Curl - Fatal编程技术网

PHP中带CURL的SOAP请求

PHP中带CURL的SOAP请求,php,soap,curl,Php,Soap,Curl,由于php.net上的SOAP手册对noob不太友好,而且我找不到任何好的示例,所以我将在这里发布我的问题 如何创建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.mywebs

由于php.net上的SOAP手册对noob不太友好,而且我找不到任何好的示例,所以我将在这里发布我的问题

如何创建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>
POST/MySERVER/myWSDLservice.asmx HTTP/1.1
主持人:connection.mywebsite.com
内容类型:text/xml;字符集=utf-8
内容长度:长度
SOAPAction:“http://connection.mywebsite.com/MySERVER/GetCarType"
一串
请注意:

  • 没有用户/通过身份验证
  • SSL连接

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

经过测试,工作正常

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

脚本运行良好,返回XML SOAP响应pastebin.com/9wzUV8Pw,但我无法将响应解析为字符串,知道吗?如果不进行测试,simplexml_load_string()无法处理“:”,对吗?我正试图使用curl使用SOAP Web服务,但我只得到一个错误,上面写着“不支持的媒体类型”。有没有办法强制内容类型标题?当我设置CURLOPT_POSTFIELDS时,内容类型更改为“application/x-www-form-urlencoded”。我在这里发现了问题!我正在使用关联数组编写标题。我只是在读到这个答案时才注意到:截至2017年2月,这项技术仍能如期发挥作用,并在人们使用肥皂时头痛时提供帮助。我只想指出一个事实,一些服务器需要xml标记的前缀(如
)(当响应包含一个关于未识别子元素的消息时,您可以看到)