【问题标题】:Soap client, how to call a function over HTTPS with Basic HTTP AuthenticationSoap 客户端,如何使用基本 HTTP 身份验证通过 HTTPS 调用函数
【发布时间】:2011-06-25 01:38:46
【问题描述】:

这个问题困扰了我一整天。

我有一个带有基本 HTTP 身份验证的客户端 Web 服务 https://*.asmx?WSDL..

如果我使用 SoapUI 连接到 web 服务,一切都会完美运行,没有任何错误..

问题是当我切换到 PHP 时...

我设法连接到它,我可以使用 php 的 soapclient 扩展从服务器获取函数列表(我使用的是 PHP 5.3.0)。

当我尝试使用 PHP 的 soapclient 扩展调用函数时,出现以下错误:

致命错误:未捕获的 SoapFault 异常:[HTTP] Error Fetching http headers in C:\www\xpto_Atestes\soapclient.php:26 堆栈跟踪:#0 [内部函数]:>SoapClient->__doRequest('***...', 'http://****...', 1, 0) #1 >C:\www\xpto_Atestes\soapclient.php(26): SoapClient->__soapCall('登录', Array) #2 {main} >在第 26 行的 C:\www\xpto_Atestes\soapclient.php 中抛出

更清晰的错误(当我对调用结果进行 print_r 时):

[faultstring] => 获取 http 标头时出错
[故障代码] => HTTP

发送以下标头:

POST /*.asmx HTTP/1.1 主机:*.pt 连接:Keep-Alive User -代理:>PHP-SOAP/5.3.0 内容类型:text/xml; charset=utf-8 SOAPAction: "http://**/Login" >Content-Length: 264 Authorization: Basic AUTHSECRETPASS

我这样称呼它:

$soapParams = array('login' => 'HTTP_LOGIN',
'密码' => 'HTTP_PASS',
'身份验证' => SOAP_AUTHENTICATION_BASIC,
'追踪' => 1,
'例外' => 0
);
$client = new SoapClient("https://****.asmx?WSDL", $soapParams); echo "已连接
"; $params = array('account' => '', 'msisdn'=>'NUMBER', 'password'=>'PASSWORD'); $result = $client->__soapCall("login", $params);

在谷歌搜索并试图找出问题所在后,我切换到了 nusoap.. 让我痛苦的是,它也给出了一个错误:

错误

HTTP 错误:标头的套接字读取超时

请求

POST /**.asmx HTTP/1.0 主机:*.pt 用户代理:NuSOAP/0.9.5 (1.123) 内容类型:文本/xml;字符集=ISO-8859-1 SOAPAction:“http://****/Login” 授权:基本 AUTHSECRETPASS 内容长度:481

数字密码

回应

我这样称呼它:

$client = new nusoap_client("https://**.asmx?WSDL", true);
$client->setCredentials('HTTPUSER','HTTPPASS','basic');
$参数 = 数组( '帐户' => '', 'msisdn'=>'NUMBER', '密码'=>'密码' );
$result = $client->call('登录', $params);

使用 nusoap,我还尝试启用 curl 扩展来拨打电话:

$client->setUseCurl(true);

但没有这样的运气,它一直报错:

HTTP 错误:cURL 错误:56:从对等方接收数据时失败 网址:http://*********.asmx
内容类型:
http_code: 0
header_size: 0
请求大小:750
文件时间:-1
ssl_verify_result: 0
重定向计数:0
总时间:21.015
名称查找时间:0
连接时间:0
pretransfer_time: 0
大小上传:462
大小下载:0
speed_download: 0
速度上传:21
下载内容长度:-1
上传内容长度:-1
开始传输时间:0
重定向时间:0

我 100% 确定 web 服务没有任何问题,如果我可以从 SoapUI 调用它而没有任何问题,我不明白为什么我不能从 PHP 调用它。

我不知道我还能做什么......我想我已经尝试了一切......

编辑:我刚刚尝试了另一个用于 PHP 的 Soap 类,这次来自 Zend..

结果是:

致命错误:未捕获的 SoapFault 异常:[WSDL] SOAP-ERROR:解析 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('登录',数组)
#5 C:\www****_Atestes\zend.php(28): Zend_Soap_Client->Login(Array)
#6 {main} 投入

【问题讨论】:

  • 你好 Tio 我有同样的问题(尝试使用 Zend 和 NuSoap 没有成功),你设法让它工作了吗?谢谢,伊夫。
  • @yjusot.. 好吧,我设法解决了NuSoap的问题,Web服务的WSDL要求各个字段都有前缀,而NuSoap没有放它们..所以我改变了NuSoap 上的代码为字段添加前缀,但我建议您使用 $client->send 方法,在该方法中您将请求 XML 作为参数传递,这样您就不必更改任何内容.. 如果您需要一个例子说点什么..
  • 您能否详细介绍一下您的解决方法/解决方案?即什么前缀?

标签: php basic-authentication soap-client nusoap


【解决方案1】:

只是回答我自己,以防其他人也想知道我是如何解决的。

问题如下,其中一个 Web 服务的请求 XML 必须采用以下格式:

<?xml version="1.0" encoding="utf-8" ?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
    <SOAP-ENV:Body>
        <ns123:IsTokenValid xmlns:ns123="http://www.xxxxxxx.com">
            <ns123:token xsi:type="xsd:string">'.$this->loginToken.'</ns123:token>
        </ns123:IsTokenValid>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

当我提到前缀时,我指的是 ns123 部分。问题是没有一个库生成正确的 XML 以发送到服务器,它们永远不会放置前缀,因此请求永远不会起作用。

我首先解决了这个问题,直接进入 NuSoap 类并在其中一个类中更改了代码,但后来我发现我可以使用 NuSoap 发出自己的请求 XML,导致以下代码:

$nsValue = rand(1000, 9000);
$requestXml = '<?xml version="1.0" encoding="utf-8" ?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns'.$nsValue.':IsTokenValid xmlns:ns'.$nsValue.'="http://www.xxxxxxx.com">
<ns'.$nsValue.':token xsi:type="xsd:string">'.$this->loginToken.'</ns'.$nsValue.':token>
</ns'.$nsValue.':IsTokenValid>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>';
$result = WebService::CallWebService($this->endPoint, 'http://www.xxxxxxx.com/IsTokenValid', $requestXml, $this->httpUser, $this->httpPass);

WebService::CallWebService 只是 NuSoap 类的一个包装器,它执行类似的操作(并且还具有错误处理、设置的 HTTP 身份验证等):

$client = new nusoap_client($webServiceLink, false);
$result = $client->send($request, $soapAction, '', self::$timeout, self::$responseTimeout);

Iain 和其他所有人,希望这很清楚。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-30
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    • 2013-07-17
    • 1970-01-01
    • 2013-09-10
    • 1970-01-01
    相关资源
    最近更新 更多