soapheader authenticate using php

ktalinu picture ktalinu · Jan 4, 2013 · Viewed 15.5k times · Source

Possible Duplicate:
PHP SoapClient and a complex header

I have this header structure:

<soapenv:Header>
  <wsse:Security xmlns:wsse=”http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd” soapenv:mustUnderstand=”0”>
    <wsse:UsernameToken>
    <wsse:Username Type=”http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#UsernameToken”>votre_login</wsse:Username>
    <wsse:Password Type=”http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText”>votre_password</wsse:Password>
    </wsse:UsernameToken>
  </wsse :Security>
</soapenv :Header>

How I put this in php using __setSoapHeaders() method?

I tried:

$auth = array(
            'UsernameToken' => array(
                'Username' => '*****',
                'Password' => '*****'
            )
        );
$header = new SoapHeader('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd','Security',$auth, 0);
$client->__setSoapHeaders($header);

and I have this error:

com.ibm.wsspi.wssecurity.SoapSecurityException: WSEC5509E: Un jeton de sécurité dont le type est [http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#UsernameToken] est requis.

Answer

Halcyon picture Halcyon · Jan 4, 2013

From: http://php.net/manual/en/soapclient.setsoapheaders.php#93460

To create complex SOAP Headers, you can do something like this:

Required SOAP Header:

<soap:Header> 
 <RequestorCredentials xmlns="http://namespace.example.com/"> 
  <Token>string</Token> 
  <Version>string</Version> 
  <MerchantID>string</MerchantID> 
  <UserCredentials> 
   <UserID>string</UserID> 
   <Password>string</Password> 
  </UserCredentials> 
 </RequestorCredentials> 
</soap:Header> 

Corresponding PHP code:

$ns = 'http://namespace.example.com/'; //Namespace of the WS. 

//Body of the Soap Header. 
$headerbody = array('Token' => $someToken, 
                'Version' => $someVersion, 
                'MerchantID'=>$someMerchantId, 
                  'UserCredentials'=>array('UserID'=>$UserID, 
                                         'Password'=>$Pwd)); 

//Create Soap Header.        
$header = new SOAPHeader($ns, 'RequestorCredentials', $headerbody);        

//set the Headers of Soap Client. 
$soap_client->__setSoapHeaders($header);