maikelsabido
maikelsabido

Reputation: 1327

SOAP Request Header is not well-formatted (Contains <item>, <key> and <value>)

The header of my SOAP Request is displayed in a weird format. I need to have a header that looks like this:

<soap-env:header>
    <wsse:security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
        <wsse:usernametoken wsu:id="UsernameToken-45">
            <wsse:username>817221</wsse:username>
            <wsse:password type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">1234</wsse:password>
        </wsse:usernametoken>
    </wsse:security>
    </soap-env:header>

Right now, the header looks like this:

<SOAP-ENV:Header>
    <ns8:Security SOAP-ENV:mustUnderstand="1">
        <item>
            <key>UsernameToken</key>
            <value>
                <item>
                    <key>Username</key>
                    <value>817221</value>
                </item>
                <item>
                    <key>Password</key>
                    <value>
                        <item>
                            <key>_</key>
                            <value>1234</value>
                        </item>
                        <item>
                            <key>Type</key>
                            <value>http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText</value>
                        </item>
                    </value>
                </item>
            </value>
        </item>
    </ns8:Security>
</SOAP-ENV:Header>

It's so wrong. It contains and tags. I've read that SOAP_ENC_OBJECT should be used to display it in correct format so I tried it in my code but still doesn't work. See the code below:

$header = array(
            'UsernameToken' => array(
                'Username' => 817221,
                'Password' => array(
                        '_' => 1234,
                        'Type' => 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText')));

$headerSoapVar = new SoapVar($header,SOAP_ENC_OBJECT); 
$soapheader = new SoapHeader('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', "Security" , $header, true);
$client->__setSoapHeaders($soapheader);

Any help would be greatly appreciated. Thanks!

Upvotes: 4

Views: 3271

Answers (2)

siniradam
siniradam

Reputation: 2929

Try this you should set it as an object.

$header = (object) array(
            'UsernameToken' => array(
                'Username' => 817221,
                'Password' => array(
                        '_' => 1234,
                        'Type' => 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText')));

Upvotes: 3

Jorge Pinho
Jorge Pinho

Reputation: 298

This is quite an old question!

However, I had this very same problem today!

I found out that "header" needs to be an object, not an array!

However I still had problems with namespaces... so I worked it out Sub-classing SoapClient class

class MySoapClient extends SoapClient {

    public function __doRequest($request, $location, $action, $version, $one_way=0) {
        // manipulate $request var using XML parse tools or whatever !!
        return parent::__doRequest($request, $location, $action, $version, $one_way);
    }
}

I am still working this out, but I hope it helps somebody !

Upvotes: 4

Related Questions