ChatGPT解决这个技术问题 Extra ChatGPT

带有 CURL 的 PHP 中的 SOAP 请求

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

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


s
sauhardnc

测试和工作!

with https, user & password < GetItemPrice xmlns="http://connecting.website.com/WSDL_Service"> // xmlns 值设置为您的 WSDL URL '.$dataFromTheForm.' '; // 来自表单的数据,例如一些 ID 号 $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: 你的操作 URL $url = $soapUrl; // 用于 https 连接的 PHP cURL 与 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); // 用户名和密码 - 在文档顶部声明 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); // SOAP 请求 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // 转换 $response = curl_exec($ch); curl_close($ch); // 转换 $response1 = str_replace("","",$response); $response2 = str_replace("","",$response1); // 将 c 转换为 XML $parser = simplexml_load_string($response2); // 用户 $parser 从 XML 响应中获取数据并显示它。 ?>


该脚本运行良好并返回 XML SOAP 响应 pastebin.com/9wzUV8Pw 但我无法将响应解析为字符串,知道吗?
如果不对其进行测试, simplexml_load_string() 就无法处理“:”,对吗?
我正在尝试使用 curl 使用 SOAP Web 服务,但我只收到一条错误消息,显示“不支持的媒体类型”。有办法强制 Content-type 标头吗?当我设置 CURLOPT_POSTFIELDS 时,内容类型更改为“application/x-www-form-urlencoded”。
我在这里发现了问题!我正在使用关联数组编写标题。我只注意到在阅读此答案时:stackoverflow.com/a/11091261/1128918
截至 2017 年 2 月,这仍然可以按预期工作,并在人们在使用 SOAP 时感到头疼时为他们提供帮助。我只想指出这样一个事实,一些服务器需要 xml 标签的前缀(如 <clin:price>)(当响应包含关于未识别子元素的消息时,您可以看到)