Doing some SOAP calls to a 3rd party application. They provide this soap header as an example of what the application expects. How can I create a SOAP header like this in PHP?
<SOAP-ENV:Header> <NS1:Security xsi:type='NS2:Security' xmlns:NS1='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd' xmlns:NS2='urn:dkWSValueObjects'> <NS2:UsernameToken xsi:type='NS2:TUsernameToken'> <Username xsi:type='xsd:string'>XXXX</Username> <Password xsi:type='xsd:string'>XXX</Password> </NS2:UsernameToken> </NS1:Security> </SOAP-ENV:Header>
I do what i think is a correct call and keep getting in return that no headers were sent.
Here is a sample from my code.
class SOAPStruct { function __construct($user, $pass) { $this->Username = $user; $this->Password = $pass; } } $client = new SoapClient('http://www.example.com/service'); $auth = new SOAPStruct('username', 'password'); $header = new SoapHeader('http://example.com/service', 'TUsernameToken', $auth); $client->__setSoapHeaders(array($header)); $client->__soapCall('GetSubscriptionGroupTypes', array(), NULL, $header)
And this is the SOAP header i get back. (its more but i stripped info away that might be sensitive)
<SOAP-ENV:Header> <ns2:TUsernameToken> <Username>username</Username> <Password>password</Password> </ns2:TUsernameToken> </SOAP-ENV:Header>
SOAP header handling in PHP is actually not very flexible and I’d go as far as saying that especially the use two namespaces within the header will make it impossible to inject the header simply by using a
SoapHeader-construct of some type.I think the best way to handle this one is to shape the XML request yourself by overriding
SoapClient::__doRequest()in a custom class that extendsSoapClient.Please see
SoapClient::__doRequestfor further information and some examples.