Sending a header using the PHP SOAP clientSending a header using the PHP SOAP client

Posted February 2nd, 2012 in PHP

I've recently been interfacing with a SOAP API on a .NET server and also creating an API in PHP, and both require sending a specific SOAP header for authentication. This post shows how to send a SOAP header using the PHP SOAP client which is built into PHP.

Sending an authentication header with PHP SOAP

In the case of the SOAP server I have been developing, the server requires a header to be sent which contains the API key. The header name is APIKey and the value the key itself. In the example below the namespace used for the API call is "ExampleNamespace".

To initiate the SOAP client and set the header using PHP do the following, where $wsdl is the URL of the WSDL file for the SOAP request:

$client = new SoapClient($wsdl);
$header = new SoapHeader('ExampleNamespace', 'APIKey', $apiKey);
$client->__setSoapHeaders($header);

Now you can make requests to the SOAP server and the header will be sent each time.

Related posts:

Comments

blog comments powered by Disqus