注册SOAP Web服务时如何设置Header?

时间:2018-11-27 06:06:13

标签: php soap nusoap soapserver

我正在使用SOAP lib(在nusoap中)为客户端创建PHP Web服务。

下面是我的代码:

    <?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);

    ?>

当我在浏览器中打开功能说明(在chrome中使用Wizdler扩展名)时,如下所示:

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

我想要的是将 Header 标记设置为 Envelope 标记的子元素,因此其外观应类似于以下内容:

    <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>

我正在努力将其设置为9小时,但找不到任何解决方案。

如果有人知道我将不胜感激。

1 个答案:

答案 0 :(得分:0)

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');
  }
}