第一步:php post方式請求webservice接口
//使用curl發送post請求
function sendCurlPost($url, $header = '', $post){
//初始化,創建一個cURL資源
$ch = curl_init();
//設置cURL選項
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "user-agent:Mozilla/5.0 (Windows NT 5.1; rv:24.0) Gecko/20100101 Firefox/24.0");
curl_setopt($ch, CURLOPT_HEADER, 0); //是否返回文件頭信息
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //不直接打印輸出
curl_setopt($ch, CURLOPT_POST, 1); //是否post請求
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); //post傳輸數據
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
//執行cURL會話
$response = curl_exec($ch);
file_put_contents("log11ss.txt", $response);
if (!curl_errno($ch)){
$result = $response;
}else{
echo 'Curl error: ' . curl_error($ch);
$result = false;
}
//關閉cURL釋放資源
curl_close($ch);
return $result;
}
$base64_str=base64_encode($food_str);//這個是請求接口傳的參數,接口要求參數是base64形式的(如果你那邊沒這個要求,就根據你那邊的要求來)
$url = "https://service/ReportService.svc";//這個是請求接口的地址
$header[] = "Content-Type: text/xml;charset=UTF-8";
$header[] = 'SOAPAction: "urn:ReportService/WEBRequest"';
$post = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<WEBRequest>
<!--Optional:-->
<strXMLParm><![CDATA['.$base64_str.']]> </strXMLParm>
</WEBRequest>
</soapenv:Body>
</soapenv:Envelope>';
$res = $this->sendCurlPost($url,$header,$post);//$res是接口返回的值
$res=utf8togb2312($res);
$res = str_ireplace('<s:', '<', $res);
$res = str_ireplace('</s:', '</', $res);
$test=new \SimpleXMLElement($res);
$return_str=base64_decode($test->Body->WEBRequestResponse->WEBRequestResult[0]);
$obj=simplexml_load_string($return_str,"SimpleXMLElement",LIBXML_NOCDATA);
$xml_object = json_decode(json_encode($obj),true);
接口返回的值res:(注意這里最好用var_dump打印返回的數據,不要用echo,print_r,不然看不到完整的數據)
string
'<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><WEBRequestResponse><WEBRequestResult>PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4NCjzmjqXlj6M+DQogIDzku6TniYw+NTBlODE1ZWZmODZhNDQ5ODkwODc4N2VjOWQ4ZDEzZDUtLTwv5Luk54mMPg0KICA85pWw5o2u57G75Z6LPjc8L+aVsOaNruexu+Weiz4NCiAgPOaTjeS9nOexu+Weiz4xPC/mk43kvZznsbvlnos+DQogIDzmk43kvZzljZXkvY0+5bm/5Lic55yB5aaH5bm85L+d5YGl6Zmi55Wq56a65YiG6ZmiPC/mk43kvZzljZXkvY0+DQogIDzmk43kvZznlKjmiLc+Z2RnenB5c2Z5PC/mk43kvZznlKjmiLc+DQogIDzmk43kvZznirbmgIE+MDwv5pON5L2c54q25oCBPg0KICA854q25oCB5o+P6L+wPuS7pOeJjOiupOivgeWksei0pe+8jOivt+iBlOezu+euoeeQhuWRmOi/m+ihjOiupOivgTwv54q25oCB5o+P6L+wPg0KPC/mjqXlj6M+</WEBRequestResult></WEBRequestResponse></s:Body></s:Envelope>' (length=720)
接下來,想獲得WEBRequestResult標簽里面的數據:
$res=utf8togb2312($res);
$res = str_ireplace('<s:', '<', $res);
$res = str_ireplace('</s:', '</', $res);
$test=new \SimpleXMLElement($res);
$return_str=base64_decode($test->Body->WEBRequestResponse->WEBRequestResult[0]);
最后一步,取解析成XML節點里面的數據:
$obj=simplexml_load_string($return_str,"SimpleXMLElement",LIBXML_NOCDATA);
$xml_object = json_decode(json_encode($obj),true);
最后附上幾個函數的說明:
SimpleXMLElement:SimpleXML 函數允許您把 XML 轉換為對象。通過普通的屬性選擇器或數組迭代器,可以處理這個對象,就像處理任何其他對象一樣。
simplexml_load_string(): 函數轉換形式良好的 XML 字符串為 SimpleXMLElement 對象。