Soap 客户端,如何使用基本 HTTP 身份验证通过 HTTPS 调用函数
这个问题困扰了我一整天。
我有一个客户端 Web 服务 https://*.asmx?WSDL,具有基本 HTTP 身份验证。
如果我使用 SoapUI 连接到 Web 服务,则一切都可以完美运行没有任何错误..
问题是当我切换到PHP时...
我设法连接到它,并且我可以使用php的soapclient扩展从服务器获取函数列表(我正在使用PHP 5.3.0)。
当我尝试使用 PHP 的soapclient 扩展调用函数时,出现以下错误:
致命错误:未捕获 SoapFault 异常:[HTTP] 获取 http 标头时出错 C:\www\xpto_Atestes\soapclient.php:26 堆栈跟踪: #0 [内部函数]: >SoapClient->__doRequest('***...', 'http:/ /****...', 1, 0) #1 >C:\www\xpto_Atestes\soapclient.php(26): SoapClient-> __soapCall('login', Array) #2 {main} > 在第 26 行的 C:\www\xpto_Atestes\soapclient.php 中抛出
更清晰的错误(当我对调用结果执行 print_r 时):
[故障字符串] =>获取 http 标头时出错
[故障代码] => HTTP
发送以下标头:
POST /*.asmx HTTP/1.1 主机:*.pt 连接:保持活动用户-代理:>PHP-SOAP/5.3.0 内容类型:text/xml; charset=utf-8 SOAPAction:“http://**/Login”>内容长度:264 授权:基本 AUTHSECRETPASS
我这样称呼它:
$soapParams = array('login' => 'HTTP_LOGIN',
'密码' => 'HTTP_PASS',
'认证' => SOAP_AUTHENTICATION_BASIC,
'踪迹' => 1、
'例外' => 0
);
$client = new SoapClient("https://****.asmx?WSDL", $soapParams); 回显“已连接
”; $params = array('帐户' => '', 'msisdn'=>'数字', '密码'=> '密码'); $result = $client->__soapCall("login", $params);
经过一段时间的谷歌搜索并试图找出问题所在后,我切换到 nusoap.. 令我痛苦的是它也给出了错误:
错误
HTTP 错误:套接字读取标头超时
请求
POST /**.asmx HTTP/1.0 主机:*.pt 用户代理:NuSOAP/0.9.5 (1.123) 内容类型:text/xml;字符集=ISO-8859-1 SOAPAction:“http://****/Login” 授权:基本 AUTHSECRETPASS 内容长度:481
数字密码
回应
我这样称呼它:
$client = new nusoap_client("https://**.asmx?WSDL", true);
$client->setCredentials('HTTPUSER','HTTPPASS','basic');
$参数=数组( '账户' => '', 'msisdn'=>'数字', '密码'=>'密码' );
$result = $client->call('登录', $params);
使用 nusoap,我还尝试启用curl 扩展来拨打电话:
$client->setUseCurl(true);
但运气不好,它一直报错:
HTTP 错误:cURL 错误:56:从对等方接收数据时失败 网址:http://*********.asmx
内容类型:
http_code: 0
标头大小:0
请求大小:750
文件时间:-1
ssl_verify_result: 0
重定向计数:0
总时间:21.015
名称查找时间:0
连接时间:0
预传输时间:0
上传大小:462
下载大小:0
下载速度:0
上传速度:21
下载内容长度:-1
upload_content_length:-1
开始传输时间:0
重定向时间:0
我百分百确定 Web 服务没有任何问题,如果我可以从 SoapUI 调用它而没有任何问题,我不明白为什么不能从 PHP 调用它。
我不知道我还能做什么..我想我尝试了一切...
编辑:我刚刚尝试了另一个 PHP 的 Soap 类,这次来自 Zend..
结果是:
致命错误:未捕获 SoapFault 异常:[WSDL] SOAP-错误:正在解析 WSDL:无法从“https://**.asmx?WSDL 加载” ' : 无法在 C:\www\xpto_Atestes\Zend\Soap\Client\Common.php 中加载外部实体“https://*.asmx?WSDL”: 51 堆栈跟踪:
#0 C:\www\xpto_Atestes\Zend\Soap\Client\Common.php(51): SoapClient->SoapClient('https://**. ..',数组)
#1 C:\www****_Atestes\Zend\Soap\Client.php(1032): Zend_Soap_Client_Common->__construct(Array, 'https:// **...',数组)
#2 C:\www****_Atestes\Zend\Soap\Client.php(1188): Zend_Soap_Client->_initSoapClientObject()
#3 C:\www***_Atestes\Zend\Soap\Client.php(1112): Zend_Soap_Client->getSoapClient() #4 [内部函数]: Zend_Soap_Client->__call('登录', Array)
#5 C:\www****_Atestes\zend.php(28): Zend_Soap_Client->Login(Array)
#6 {main} 投入
This problem has been killing me for the entire day.
I have a client web service https://*.asmx?WSDL, with Basic HTTP Authentication..
If I use SoapUI to connect to the webservice, everything works perfectly with no errors whatsoever..
The problem is when I switch to PHP...
I managed to connect to it, and I can get the function list from the server using soapclient extension for php ( I'm using PHP 5.3.0 ).
When I try to call a function using the soapclient extension for PHP I get the following error:
Fatal error: Uncaught SoapFault exception: [HTTP] Error Fetching http headers in
C:\www\xpto_Atestes\soapclient.php:26 Stack trace: #0 [internal function]: >SoapClient->__doRequest('***...', 'http://****...', 1, 0) #1 >C:\www\xpto_Atestes\soapclient.php(26): SoapClient->__soapCall('login', Array) #2 {main} >thrown in C:\www\xpto_Atestes\soapclient.php on line 26
More clear error ( when I do a print_r of the result of the call):
[faultstring] => Error Fetching http headers
[faultcode] => HTTP
With the following headers being sent:
POST /*.asmx HTTP/1.1 Host: *.pt Connection: Keep-Alive User-Agent: >PHP-SOAP/5.3.0 Content-Type: text/xml; charset=utf-8 SOAPAction: "http://**/Login" >Content-Length: 264 Authorization: Basic AUTHSECRETPASS
I'm calling it like this:
$soapParams = array('login' => 'HTTP_LOGIN',
'password' => 'HTTP_PASS',
'authentication' => SOAP_AUTHENTICATION_BASIC,
'trace' => 1,
'exceptions' => 0
);
$client = new SoapClient("https://****.asmx?WSDL", $soapParams);
echo "Connected
";
$params = array('account' => '', 'msisdn'=>'NUMBER', 'password'=>'PASSWORD');
$result = $client->__soapCall("login", $params);
After a while googling and trying to find out what the problem could be, I switched to nusoap.. which to my misery it gave an error also:
Error
HTTP Error: socket read of headers timed out
Request
POST /**.asmx HTTP/1.0
Host: *.pt
User-Agent: NuSOAP/0.9.5 (1.123)
Content-Type: text/xml; charset=ISO-8859-1
SOAPAction: "http://****/Login"
Authorization: Basic AUTHSECRETPASS
Content-Length: 481NUMBERPASSWORD
Response
I'm calling it like this:
$client = new nusoap_client("https://**.asmx?WSDL", true);
$client->setCredentials('HTTPUSER','HTTPPASS','basic');
$params = array(
'account' => '',
'msisdn'=>'NUMBER',
'password'=>'PASSWORD'
);
$result = $client->call('Login', $params);
With nusoap I also tried to enable enable curl extension to make the calls:
$client->setUseCurl(true);
But no such luck, it kept on giving an error:
HTTP Error: cURL ERROR: 56: Failure when receiving data from the peer
url: http://*********.asmx
content_type:
http_code: 0
header_size: 0
request_size: 750
filetime: -1
ssl_verify_result: 0
redirect_count: 0
total_time: 21.015
namelookup_time: 0
connect_time: 0
pretransfer_time: 0
size_upload: 462
size_download: 0
speed_download: 0
speed_upload: 21
download_content_length: -1
upload_content_length: -1
starttransfer_time: 0
redirect_time: 0
I'm 100% sure that nothing is wrong with the webservice, if I can call it from SoapUI without any problem I don't see why I can't call it from PHP.
I rally don't know what more I can do.. I think I tried everything...
Edit: I just tried another Soap Class for PHP, this time from Zend..
The result is:
Fatal error: Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: Couldn't load from 'https://**.asmx?WSDL' : failed to load external entity "https://*.asmx?WSDL" in C:\www\xpto_Atestes\Zend\Soap\Client\Common.php:51 Stack trace:
#0 C:\www\xpto_Atestes\Zend\Soap\Client\Common.php(51): SoapClient->SoapClient('https://**...', Array)
#1 C:\www****_Atestes\Zend\Soap\Client.php(1032): Zend_Soap_Client_Common->__construct(Array, 'https://**...', Array)
#2 C:\www****_Atestes\Zend\Soap\Client.php(1188): Zend_Soap_Client->_initSoapClientObject()
#3 C:\www***_Atestes\Zend\Soap\Client.php(1112): Zend_Soap_Client->getSoapClient() #4 [internal function]: Zend_Soap_Client->__call('Login', Array)
#5 C:\www****_Atestes\zend.php(28): Zend_Soap_Client->Login(Array)
#6 {main} thrown in
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是回答我自己,以防其他人也想知道我是如何解决的。
问题如下,其中一个 Web 服务的请求 XML 必须采用以下格式:
当我提到前缀时,我指的是 ns123 部分。问题在于,没有一个库生成正确的 XML 来发送到服务器,它们永远不会添加前缀,因此请求永远不会起作用。
我首先解决了这个问题,直接进入 NuSoap 类并更改了其中一个类中的代码,但后来我发现我可以使用 NuSoap 发出自己的请求 XML,结果产生了以下代码:
WebService::CallWebService 只是一个NuSoap 类的包装器,它执行类似的操作(并且还具有错误处理、设置 HTTP 身份验证等):
Iain 和其他人,希望这一点很清楚。
Just to answer myself, in case anyone else would like to also know how I solved it.
The problem was the following, the request XML for one of the webservices had to be in this format:
When I mention the prefix, I'm referring to the ns123 part. The problem is that none of the libraries produced the correct XML to send to the server, they would never put the prefix, so the request never worked.
I first solved it, by going directly to the NuSoap class and changed the code in one the classes, but then I discovered I could make my own request XML with NuSoap, witch resulted in the following code:
The WebService::CallWebService is just a wrapper to NuSoap class that does something like this ( and also has error handling, set's HTTP authentication etc ):
Iain and everyone else, hope this is clear.