PHP Cookbook: Solutions and Examples for PHP Programmers
15.7.1. Problem
You want to emit a SOAP header from your SOAP server. 15.7.2. Solution
Call the addSoapHeader( ) method: <?php class pc_SOAP_return_time { public function return_time() { $tz = date_default_timezone_get(); $header = new SoapHeader('urn:pc_SOAP_return_time', 'get_timezone', $tz) $GLOBALS['server']->addSoapHeader($header); return date('Ymd\THis'); } } $server = new SOAPServer(null, array('uri'=>'urn:pc_SOAP_return_time')); $server->setClass('pc_SOAP_return_time'); $server->handle(); ?>
This adds the following XML to the SOAP response: <SOAP-ENV:Header><ns1:get_timezone>America/Los Angeles</ns1:get_timezone> </SOAP-ENV:Header> 15.7.3. Discussion
It's typical to process SOAP headers sent by a SOAP client. That's the subject of Recipe 15.6. However, you can also create new SOAP headers from within your SOAP server and send them back to the client. This breaks down into two steps:
Example 15-10 shows how that's implemented in PHP. Sending a SOAP header from a SOAP server
In Example 15-10, the pc_SOAP_return_time( ) method creates a new SOAP header named get_timezone that contains the default time zone. This header is then added to the reply by calling $GLOBALS['server']->addSoapHeader($header);. Since there's no easy way to access the $server object from within the scope of the method, you access it directly through the $GLOBALS array. Now, when ext/soap responds, it will embed the SOAP header in its reply. For example: <SOAP-ENV:Header><ns1:get_timezone>America/Los Angeles</ns1:get_timezone> </SOAP-ENV:Header>
It's then up to the SOAP client to decide how to process this header. You can access it using SOAPClient by passing a fifth argument to __soapCall( ): $result = $client->__soapCall('return_time', array(), array(), array(), $output); print_r($output); Array ( [timezone] => America/Los_Angeles )
15.7.4. See Also
Recipe 15.6 for processing a SOAP header; Recipe 26.1 for using a a SOAP header; documentation on SOAPHeader at the following address: http://www.php.net/manual/function.soap-soapheader-construct.php. |
Категории