PHP post方式请求webservice接口以及解析返回的数据

第一步: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 对象。

原文地址:https://www.cnblogs.com/hupengyin/p/13953672.html