最近需要通过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));
但改了之后执行无任何返回又无错误,执行很久后会提示超时。
不知道什么原因。
先谢过各位回复的大侠。
回复讨论(解决方案) 因为你给出的地址无法访问,所以不能知道 wsdl 都说了什么
当然用于验证用户的 soapheader 参数也不会出现在 wsdl 中
你可以联系服务的提供者,确认 soapheader 的参数是否正确
谢谢xuzuning版主的回复,上面示例代码中的地址是假的,我测试时用的是一个可以访问的地址,那么剩下就只可能是soapheader中参数的问题了,假如参数有误,webservice ssl验证不通过,不会返回验证不通过之类的提示,就一直是没有任何返回的吗?我们的服务的提供者比较难联系,通过用户名密码的https验证一般是怎么写的?下面这样的写法一般可以怎么修改?
$header = new soapheader(https://myurl.svc, authentication, array('username'=>$username,'password'=>$password), false);
$client->__setsoapheaders(array($header));
你的写法是正确的,只要参数是正确的就行
确认了,username和password的值是没问题的,不过username里面有空格和:’等字符,不知道是不是要处理一下才能正常传输过去?如果要处理的话,怎么处理才行呢?
下面中括号中是username的值
【abcdefg||thu, 14 mar 2013 02:30:09 gmt】
你的用户名是随时随刻变的吗?
是的,密码也是时刻变的,有一定的加密方式,时间验证的话有几分钟的容忍范围
改用curl访问试试,还是失败,不知道是不是我传参数的方式有误,错误提示和代码是:
====错误提示=====
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 =
;
$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
tudata
;
print_r($tudata);
有大侠有这块经历的吗?给点提示也行。。。先谢谢了
php的soapclient到底能支持soap 1.2吗?为什么soapclient指定1.2版本后,请求无反馈?
楼主 我也 碰到这个问题 请问 楼主就解决了没有