最近需要通过PHP访问一个.net的webservice,代码如下:
$client = new SoapClient("https://myurl.svc?wsdl",array('soap_version'=> SOAP_1_1,'trace' => TRUE,'exceptions' => FALSE));
$header = new SoapHeader("https://myurl.svc", "Authentication", array('UserName'=>$username,'Password'=>$password), false);
$client->__setSoapHeaders(array($header));
$client->GetVersion();//这是webservice的一个方法
出错:
PHP Fatal error: Uncaught SoapFault exception: [HTTP] Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'查了一些资料后说php的soapclient 默认是使用1.1版本发送,content_type 是 "text/xml",改成1.2版本
content_type 才是 "application/soap+xml",所以做修成以下
$client = new SoapClient("https://myurl.svc?wsdl",array('soap_version'=> SOAP_1_2,'trace' => TRUE,'exceptions' => FALSE));
但改了之后执行无任何返回又无错误,执行很久后会提示超时。
不知道什么原因。
先谢过各位回复的大侠。
$client = new SoapClient("https://myurl.svc?wsdl",array('soap_version'=> SOAP_1_1,'trace' => TRUE,'exceptions' => FALSE));
$header = new SoapHeader("https://myurl.svc", "Authentication", array('UserName'=>$username,'Password'=>$password), false);
$client->__setSoapHeaders(array($header));
$client->GetVersion();//这是webservice的一个方法
出错:
PHP Fatal error: Uncaught SoapFault exception: [HTTP] Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'查了一些资料后说php的soapclient 默认是使用1.1版本发送,content_type 是 "text/xml",改成1.2版本
content_type 才是 "application/soap+xml",所以做修成以下
$client = new SoapClient("https://myurl.svc?wsdl",array('soap_version'=> SOAP_1_2,'trace' => TRUE,'exceptions' => FALSE));
但改了之后执行无任何返回又无错误,执行很久后会提示超时。
不知道什么原因。
先谢过各位回复的大侠。
当然用于验证用户的 SoapHeader 参数也不会出现在 WSDL 中
你可以联系服务的提供者,确认 SoapHeader 的参数是否正确
$header = new SoapHeader("https://myurl.svc", "Authentication", array('UserName'=>$username,'Password'=>$password), false);
$client->__setSoapHeaders(array($header));
下面中括号中是username的值
【abcdefg||Thu, 14 Mar 2013 02:30:09 GMT】
====错误提示=====
Took 1.530147 seconds to send a request to https://****.svc?wsdl
tuData
http://www.w3.org/2005/08/addressing/soap/faults:Sendera:BadContextTokenThe security context token is expired or is not valid. The message was not processed.
====代码====
$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>
</soap12:Body>
</soap12:Envelope>";
$tuCurl = curl_init();
curl_setopt($tuCurl, CURLOPT_URL, "https://****.svc?wsdl");
$curlPost="Username=" . $username . '&Password=' . $password;
curl_setopt($tuCurl, CURLOPT_POST, 1);
curl_setopt($tuCurl, CURLOPT_POSTFIELDS,$curlPost);
curl_setopt($tuCurl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($tuCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($tuCurl, CURLOPT_POSTFIELDS, $data);
curl_setopt($tuCurl, CURLOPT_HTTPHEADER, array("Content-Type: application/soap+xml; charset=utf-8"));
$tuData = curl_exec($tuCurl);
if(!curl_errno($tuCurl)){
$info = curl_getinfo($tuCurl);
echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];
} else {
echo 'Curl error: ' . curl_error($tuCurl);
}
curl_close($tuCurl);
print "<br>tuData<br>";
print_r($tuData);有大侠有这块经历的吗?给点提示也行先谢谢了