rannt
rannt

Reputation: 85

How to submit SOAP request with auth headers in PHP?

I'm using a SOAP webservice (https://servisi.bisnode.si/BisnodeWebService/BisnodeWebService.asmx?WSDL)

(the method is described here: https://servisi.bisnode.si/BisnodeWebService/BisnodeWebService.asmx?op=GetData_PodjetjeReport)

Until now service was accessed over insecure connection (http://servisi.gvin.com), but the provider switched over to https (https://servisi.bisnode.si).

This is my PHP code:

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

When I use $insecure, this is how the request looks like:

====== 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
"

In this case, everything works as expected. But when I use $secure, this is what happens:

====== 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
"

Notice the REQUEST part, where there are additional "Username" and "Password" parts included in the string.

This results in failed authentication.

Why the difference and how to fix it?

I tried adding different params to soap request but no success.

Thank you for your help!

-- EDIT --

With the help offered by @Daniel O. I managed to do a step forward.

This is the XML I get, when I use $secure:

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

I don't know why, but there is an additional namespace created. I guess this would not be a problem, if the Username and Password tags would be prefixed with ns2, just like the AuthHeader is.

Now this is what I get, when using $insecure (this works):

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

How to fix this?

Upvotes: 0

Views: 3768

Answers (2)

nunar
nunar

Reputation: 1

It isn't necessary to use stdClass for AuthHeader. It should work with 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);

Header 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>

Upvotes: 0

odan
odan

Reputation: 4952

The AuthHeader data must be a stdClass (and not an array).

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

Header result:

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

Upvotes: 1

Related Questions