如何在PHP中使用auth头提交SOAP请求?

时间:2018-01-09 20:04:21

标签: php web-services soap https

我正在使用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部分,其中有其他"用户名"和#34;密码"包含在字符串中的部分。

这会导致身份验证失败。

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

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

感谢您的帮助!

- 编辑 -

在@Daniel O的帮助下,我设法向前迈进了一步。

当我使用$ 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>

如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

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>

答案 1 :(得分:0)

使用stdClass进行AuthHeader是没有必要的。它应该与SoapVar一起使用。

BigDecimal

标题结果:

<?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);