We help IT Professionals succeed at work.

How to add soap header in response using PHP extension?

activesoftware
activesoftware used Ask the Experts™
on
Hi,

I am creating SOAP server using PHP extension I would like to know how can I add soap header in response.

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Have you read the SoapHeader section of the php manual?

SoapHeader::SoapHeader  (  string $namespace  ,  string $name  [,  mixed $data  [,  bool $mustunderstand  [,  string $actor  ]]] )

Parameters

namespace: The namespace of the SOAP header element.
name: The name of the SoapHeader object.
data: A SOAP header's content. It can be a PHP value or a SoapVar object.
mustUnderstand: Value of the mustUnderstand attribute of the SOAP header element.
actor: Value of the actor attribute of the SOAP header element.

SoapHeader::SoapHeader() example
<?php
$client = new SoapClient(null, array('location' => "http://localhost/soap.php",
                                     'uri'      => "http://test-uri/"));
$client->__soapCall("echoVoid", null, null,
                new SoapHeader('http://soapinterop.org/echoheader/',
                               'echoMeStringRequest',
                               'hello world'));
?>

Open in new window

Commented:
This section covers adding a header to the response:-

public void SoapServer::addSoapHeader  (  SoapHeader $object  )

http://www.php.net/manual/en/soapserver.addsoapheader.php

Author

Commented:
Hi,

Thanks for replying , I have tried with addSoapHeader as in the below code , but I get following error:

Fatal error: Uncaught SoapFault exception: [SOAP-ENV:Server] SoapServer::addSoapHeader() [function.SoapServer-addSoapHeader]: The SoapServer::addSoapHeader function may be called only during SOAP request processing in E:\phpcode\soappoc\client.php:17 Stack trace: #0 E:\phpcode\soappoc\client.php(17): SoapClient->__soapCall('SomeFunctionOfT...', Array) #1 {main} thrown in E:\phpcode\soappoc\client.php on line 17

Thanks
<?php
$hdr = file_get_contents("php://input");
if (strpos($hdr,'<SOAP-ENV:Header>')===false) {
    $hdr = null;
} else {
    $hdr = explode('<SOAP-ENV:Header>',$hdr);
    $hdr = explode('</SOAP-ENV:Header>',$hdr[1]);
    $hdr = $hdr[0];
}
$options = array('uri' => 'http://127.0.0.1/soappoc/');
$srv = new SoapServer(NULL, $options);
$srv->setClass("ServiceClass",$hdr);
$srv->handle();
class ServiceClass {
    var $IsAuthenticated;
    var $header;

    function __construct($hdr) {
        $this->IsAuthenticated = false;
        if ($hdr!=null) {
            $this->header = simplexml_load_string($hdr);
            //Your authentication stuff goes here...
        }
    }
    
    function SomeFunctionOfTheService($a) {
       // if ($this->IsAuthenticated) {
            //here Your function...
			 // Prepare SoapHeader parameters 
			 $sh_param = array( 
								'Username'    =>    'sapna', 
								'Password'    =>    'talreja'); 

			 $headers = new SoapHeader('http://127.0.0.1/soappoc/server.php', 'UserCredentials', $sh_param,true); 
			 SoapServer::addSoapHeader($headers);
            return 1;
     //   }
    }

}

Open in new window

Most Valuable Expert 2011
Top Expert 2016

Commented:
Let me take this question in a slightly different direction.  WHY would you want to use SOAP when you might design a RESTful interface instead?  REST is easier to code and document - perhaps by orders of magnitude - when compared to SOAP.  I see hundreds of questions each year from developers who cannot get a SOAP interface to work.  That never happens with REST.

A good example of a RESTful interface is the Yahoo Geocoder.  See the example here, and then decide if you're adding any value by using SOAP, or maybe just adding overhead and complexity.

http://developer.yahoo.com/maps/rest/V1/geocode.html

HTH, ~Ray

Author

Commented:
HI,

Thanks for your advice I agree , but using SOAP is complexion as the request is already defined and it is SOAP request so I am left with no choice.

Thanks

Most Valuable Expert 2011
Top Expert 2016

Commented:
OK, good luck with it, you will need it!

;-)

Best, ~Ray