Recipe 14.7. Using SOAP Headers


14.7.1. Problem

You need to create a SOAP header and pass it along with your request. This is often a place where a service requires authentication credentials or other information not directly related to the request.

14.7.2. Solution

Use the SOAPHeader class to create the header.

To use the same headers for all requests to a service, call __setSoapHeaders( ):

$client = new SOAPClient('http://www.example.com/service.wsdl'); $username = new SOAPHeader('urn:service-namespace', 'Username', 'elvis'); $password = new SOAPHeader('urn:service-namespace', 'Password', 'the-king'); $headers = array($username, $password); $client->__setSoapHeaders($headers); 

You can also pass in headers on a per-call basis as the fourth argument to __soapCall( ) :

$client = new SOAPClient('http://www.example.com/service.wsdl'); $username = new SOAPHeader('urn:service-namespace', 'Username', 'elvis'); $password = new SOAPHeader('urn:service-namespace', 'Password', 'the-king'); $headers = array($username, $password); $client->__soapCall($function, $args, $options, $headers);

This creates XML that looks like this:

<SOAP-ENV:Header>     <ns2:Username>elvis</ns2:Username>     <ns2:Password>the-king</ns2:Password> </SOAP-ENV:Header>

The namespace prefix may vary, but it will be mapped to the urn:service-namespace namespace URI.

14.7.3. Discussion

The SOAP envelope is divided into two parts, a SOAP header and a SOAP body. This division is similar to how HTTP has a header and a body. Most of the time you only need to access the body, but sometimes you also need to set headers, too.

The ext/soap extension does an excellent job making it simple to pass in data that's part of the SOAP body. However, while it supports everything you need to create SOAP headers, it doesn't make it easy.

Depending on the design of the SOAP headers, it can be of varying difficulty to create what you need. When all you need is data wrapped around elements, create SOAPHeader objects and package them in an array:

$client = new SOAPClient('http://www.example.com/service.wsdl'); $username = new SOAPHeader('urn:service-namespace', 'Username', 'elivs'); $password = new SOAPHeader('urn:service-namespace', 'Password', 'the-king'); $headers = array($username, $password); 

These headers can then be added on all requests for a particular SOAP client instance or on a per-call basis:

$client = new SOAPClient('http://www.example.com/service.wsdl'); // Use __setSoapHeaders() to add header to *all* requests $client->__setSoapHeaders($headers); // Or do it on a per-call basis $client->__soapCall($function, $args, $options, $headers);

This adds the following XML to your request:

<SOAP-ENV:Header>     <ns2:Username>elivs</ns2:Username>     <ns2:Password>the-king</ns2:Password> </SOAP-ENV:Header>

In your case, the namespace prefix may be something other than ns2, but it will be mapped to the urn:service-namespace namespace URI.

14.7.4. See Also

Recipe 15.6 for processing SOAP headers and Recipe 15.7 for sending SOAP headers from a SOAP server.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net