用 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 都是试了好几次,才终于成功。
比较要注意的几个点:
第一次接触 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