如何在 PHP 中提交带有身份验证标头的 SOAP 请求?



我正在使用 SOAP 网络服务 (https://servisi.bisnode.si/BisnodeWebService/BisnodeWebService.asmx?WSDL)

(该方法描述如下:https://servisi.bisnode.si/BisnodeWebService/BisnodeWebService.asmx?op=GetData_PodjetjeReport)

到目前为止,服务是通过不安全的连接(http://servisi.gvin.com)访问的,但提供商切换到https(https://servisi.bisnode.si)。

这是我的PHP代码:

$insecure = 'http://servisi.gvin.com';
$secure = 'https://servisi.bisnode.si';
$soapClient = new SoapClient($insecure."/BisnodeWebService/BisnodeWebService.asmx?WSDL", array('trace' => 1, 'exceptions' => 0));
$header = new SoapHeader($insecure.'/BisnodeWebService', 'AuthHeader', array('Username' => '[user]', 'Password' => '[pass]'));
$soapClient->__setSoapHeaders($header);
$result = $soapClient->GetData_PodjetjeReport(array('sMSorDS' => '1234567'));
echo "====== REQUEST HEADERS =====" . PHP_EOL;
var_dump($soapClient->__getLastRequestHeaders());
echo "========= REQUEST ==========" . PHP_EOL;
var_dump($soapClient->__getLastRequest());
echo "========= RESPONSE ==========" . PHP_EOL;
var_dump($result);

当我使用 $insecure 时,请求如下所示:

====== REQUEST HEADERS =====
string(276) "POST /BisnodeWebService/BisnodeWebService.asmx HTTP/1.1
Host: servisi.gvin.com
Connection: Keep-Alive
User-Agent: PHP-SOAP/5.5.38
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://servisi.gvin.com/BisnodeWebService/GetData_PodjetjeReport"
Content-Length: 460
========= REQUEST ==========
string(460) "
[user][pass]1234567
"

在这种情况下,一切都按预期工作。 但是当我使用$secure时,就会发生这种情况:

====== REQUEST HEADERS =====
string(278) "POST /BisnodeWebService/BisnodeWebService.asmx HTTP/1.1
Host: servisi.bisnode.si
Connection: Keep-Alive
User-Agent: PHP-SOAP/5.5.38
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://servisi.gvin.com/BisnodeWebService/GetData_PodjetjeReport"
Content-Length: 553
========= REQUEST ==========
string(553) "
Username[user]Password[pass]1234567
"

请注意 REQUEST 部分,其中字符串中包含其他"用户名"和"密码"部分。

这会导致身份验证失败。

为什么会有差异以及如何解决它?

我尝试在肥皂请求中添加不同的参数,但没有成功。

感谢您的帮助!

--编辑--

在@Daniel·我设法向前迈出了一步。

这是我使用 $secure 时得到的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://servisi.gvin.com/BisnodeWebService" xmlns:ns2="https://servisi.bisnode.si/BisnodeWebService">
<SOAP-ENV:Header>
<ns2:AuthHeader>
<Username>[user]</Username>
<Password>[pass]</Password>
</ns2:AuthHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:GetData_PodjetjeReport>
<ns1:sMSorDS>1234567</ns1:sMSorDS>
</ns1:GetData_PodjetjeReport>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我不知道为什么,但是创建了一个额外的命名空间。我想这不会是问题,如果用户名和密码标签以 ns2 为前缀,就像 AuthHeader 一样。

现在这就是我在使用$insecure时得到的(这有效):

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://servisi.gvin.com/BisnodeWebService">
<SOAP-ENV:Header>
<ns1:AuthHeader>
<ns1:Username>[user]</ns1:Username>
<ns1:Password>[pass]</ns1:Password>
</ns1:AuthHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:GetData_PodjetjeReport>
<ns1:sMSorDS>1234567</ns1:sMSorDS>
</ns1:GetData_PodjetjeReport>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

如何解决这个问题?

AuthHeader 数据必须是 stdClass(而不是数组)。

$soapClient = new SoapClient('https://servisi.bisnode.si/BisnodeWebService/BisnodeWebService.asmx?WSDL', [
'trace' => 1,
'exceptions' => 0,
]);
// Must be a stdClass (and not an array)
$auth = new stdClass();
$auth->Username = '[username]';
$auth->Password = '[password]';
$header = new SoapHeader('http://servisi.gvin.com/BisnodeWebService', 'AuthHeader', $auth);
$soapClient->__setSoapHeaders($header);
$result = $soapClient->GetData_PodjetjeReport(array('sMSorDS' => '1234567'));
echo "====== REQUEST HEADERS =====" . PHP_EOL;
var_dump($soapClient->__getLastRequestHeaders());
echo "========= REQUEST ==========" . PHP_EOL;
var_dump($soapClient->__getLastRequest());
echo "========= RESPONSE ==========" . PHP_EOL;
var_dump($result);

标头结果:

<SOAP-ENV:Header><ns1:AuthHeader><ns1:Username>[username]</ns1:Username><ns1:Password>[password]</ns1:Password></ns1:AuthHeader></SOAP-ENV:Header>

没有必要将 stdClass 用于 AuthHeader。它应该与SoapVar一起使用。

<?php
$insecure = 'http://servisi.gvin.com';
$secure = 'https://servisi.bisnode.si';
$soapClient = new SoapClient($secure."/BisnodeWebService/BisnodeWebService.asmx?WSDL", array('trace' => 1, 'exceptions' => 0));
$auth = new SoapVar(array('ns2:Username' => '[user]', 'ns2:Password' => '[pass]'), SOAP_ENC_OBJECT);
$header = new SoapHeader($secure.'/BisnodeWebService', 'AuthHeader', $auth);
$soapClient->__setSoapHeaders($header);
$result = $soapClient->GetData_PodjetjeReport(array('sMSorDS' => '1234567'));
echo "====== REQUEST HEADERS =====" . PHP_EOL;
var_dump($soapClient->__getLastRequestHeaders());
echo "========= REQUEST ==========" . PHP_EOL;
var_dump($soapClient->__getLastRequest());
echo "========= RESPONSE ==========" . PHP_EOL;
var_dump($result);

标头结果:

========= REQUEST ==========
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://servisi.gvin.com/BisnodeWebService" xmlns:ns2="https://servisi.bisnode.si/BisnodeWebService">
<SOAP-ENV:Header>
<ns2:AuthHeader>
<ns2:Username>[user]</ns2:Username>
<ns2:Password>[pass]</ns2:Password>
</ns2:AuthHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:GetData_PodjetjeReport>
<ns1:sMSorDS>1234567</ns1:sMSorDS>
</ns1:GetData_PodjetjeReport>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

相关内容

  • 没有找到相关文章

最新更新