PHP curl 串接一个 SOAP 的 API

用 php curl 串接 SOAP API

$headers[]="Content-Type: application/soap+xml; charset=utf-8"; $data='<info><name>Your name</name><age>Your age</age></info>'; // 资料$url = "http:/xxx/xxx.asmx"; // API URL$string=htmlspecialchars($data); // 先编码// soap $data = '<?xml version="1.0" encoding="utf-8"?><soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">  <soap12:Body>    <Data xmlns="zzz">      <xml>'.$string.'</xml>    </Data>  </soap12:Body></soap12:Envelope>'; $curl = curl_init($url);curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLOPT_POST, true);curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);curl_setopt($curl, CURLOPT_POSTFIELDS, $data);curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);$response = curl_exec($curl);$soap=simplexml_load_string($response); $Data_XmlResult = $soap->children('http://www.w3.org/2003/05/soap-envelope')->Body->children()->Data_Response; // 解析 response 的 soap,看要取的资料在哪一层就箭头到哪$info=simplexml_load_string($Data_XmlResult); // 做成物件

API 的 request 跟 response 都是试了好几次,才终于成功。
比较要注意的几个点:

header 带的 Content-Type资料的格式(原本以为不用编码直觉写 XML 即可,后来发现要编码才可以成功)如何解析 reseponse 的 SOAP(解析不了就会取不到想要的 data)

第一次接触 soap,完全没概念,只能一直看别人的範例和各种尝试才终于成功

参考资料:
https://stackoverflow.com/questions/20393140/how-to-read-soap-response-xml-in-php
https://stackoverflow.com/questions/7120586/soap-request-in-php-with-curl


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章