How to set Header while registering SOAP web service?

390 Views Asked by At

I am creating SOAP web service for client using nusoap lib(in PHP).

Below is my code:

    <?php


    require_once('include/nusoap/nusoap.php');

    function testFunction($msg){
        return $msg;
    }

    $server = new soap_server();

    $namespace = "http://www.test-site.com";    
    $server->configureWSDL("TEST_FUNCTION_SERVICE",$namespace);


    $server->register("testFunction",      // Register Method
     array('msg' => 'xsd:string'),    // Method Parameter
     array('return' => 'xsd:string'), // Response
     $namespace,                      // Namespace
     false,                           // soapaction: (use default)
     "rpc",                           // style: rpc or document
     "encoded",                       // use: encoded or literal
     "This is TEST Function"               // description: documentation for the method
    );

    $POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';

    @$server->service($POST_DATA);

    ?>

When I open up function description in browser (using Wizdler extension in chrome), it looks like below:

    <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
        <Body>
            <testFunction xmlns="http://www.test-site.com">
                <msg>[string]</msg>
            </testFunction>
        </Body>
    </Envelope>

What I want is to set Header tag as child element to Envelope tag, so it should looks like something below:

    <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
        <Header>
            <Authenticate_Info xmlns="http://www.test-site.com">
                <UserName>[string?]</UserName>
                <Password>[string?]</Password>
                <EntityId>[int]</EntityId>
            </Authenticate_Info>
        </Header>
        <Body>
            <testFunction xmlns="http://www.test-site.com">
                <msg>[string]</msg>
            </testFunction>
        </Body>
    </Envelope>

I am struggling to set it for 9 Hrs, but couldn't find any solution.

I would be grateful if someone knows this.

1

There are 1 best solutions below

1
On
function testFunction($msg){
  global $server;

  $UserName = $server->requestHeader['Authenticate_Info']['UserName'];
  $Password = $server->requestHeader['Authenticate_Info']['Password'];
  $EntityId = $server->requestHeader['Authenticate_Info']['EntityId'];
  if ($username=='xxxxx' and  $password=='xxxxx' and $EntityId=='xxxxx') {
    return $msg;
  }else{
    return new nusoap_fault('SOAP-ENV:Client', '', 'Authentication failed');
  }
}