-->

NuSoap PHP webservice with soap headers

2019-07-22 20:12发布

问题:

I am wondering how to implement soap header authentication in a soap web service server using NuSoap library.

I have seen lots of examples about NuSoap Client but want to implement this in a server.

Thank you, mk

回答1:

    $client->setHeaders('<wsse:Security S:mustUnderstand="1">
            <wsu:Timestamp xmlns:ns15="http://schemas.xmlsoap.org/ws/2006/02/addressingidentity" xmlns:ns14="http://docs.oasis-open.org/ws-sx/ws-secureconversation/200512" xmlns:ns13="http://www.w3.org/2003/05/soap-envelope" wsu:Id="_1">
                <wsu:Created>createdDate</wsu:Created>
                <wsu:Expires>expireDate</wsu:Expires>
            </wsu:Timestamp>
            <wsse:UsernameToken xmlns:ns15="http://schemas.xmlsoap.org/ws/2006/02/addressingidentity" xmlns:ns14="http://docs.oasis-open.org/ws-sx/ws-secureconversation/200512" xmlns:ns13="http://www.w3.org/2003/05/soap-envelope" wsu:Id="uuid_25007e25-6a0a-4a0c-9c3e-0d332f262cdc">
                <wsse:Username>username</wsse:Username>
                <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">password</wsse:Password>
            </wsse:UsernameToken>
        </wsse:Security>');


回答2:

The soapserver object holds the soap header as an associative array under the 'requestHeader' property, so if you can figure out a way to get an instance of the server from your function, you will be able to get the soapHeader.

<?php
require_once './nusoap/nusoap.php';

//Declare the server as a global, for brevity
global $server;

//Instantiate, configure and run as usual
$server = new nusoap_server();
$server->configureWSDL("namespace...", "...");
$server->register("myHandler");
$server->service(isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '');

//My handling function:
function myHandler() {
    //Get your server instance:
    global $server;

    //Abra Kadabra alakazam! your soap header :D
    var_dump($server->requestHeader);
}

There is obviously better coding practices for doing this, but you got the idea. Also whatchout for the requestHeaders property, as it holds the HTTP headers NOT the SOAP Header, remember: requestHeader WITHOUT the trailing 's' is your guy.

Hope it helps