views:

1780

answers:

3

I'm creating a web service using PHP5's native SOAP Methods. Everything went fine until I tried to handle authentication using SOAP Headers.

I could easily find how to add the username/password to the SOAP headers, client-side:

$myclient = new SoapClient($wsdl, $options);

$login = new SOAPHeader($wsdl, 'email', 'mylogin');
$password = new SOAPHeader($wsdl, 'password', 'mypassword');
$headers = array($login, $password);

$myclient->__setSOAPHeaders($headers);

But I can't find anywhere the methods for collecting and processing these headers server-side. I'm guessing there has to be an easy way to define a method in my SoapServer that handles the headers...

+4  A: 

SoapClient uses the username and password to implement HTTP authentication. Basic and Digest authentication are support (see source)

For information on implementing HTTP authentication in PHP on the server side, see this manual page.

If you don't want to use HTTP authentication, see this user-contributed sample on the SoapServer manual page which shows how you could pass some credentials in a UsernameToken header.

Paul Dixon
I am trying to catch headers using just the SOAP Library, so without HTTP authentification. And I've tried the user-contributed sample you mentioned, but it only seems to work when I'm *not* using a WSDL Definition.Maybe I have to define the headers in the WSDL, I'll keep searching. Thank you for your input tho.
altermativ
A: 

@alternative

Did you ever find a solution for how to make the user-contributed sample work when using a WSDL definition?

selimnairb
A: 

You can try reading RAW POST data.

if ( $_SERVER['REQUEST_METHOD'] == 'POST' )
{
    $xml = file_get_contents('php://input');
    print( htmlspecialchars( $xml );
    // XML processing
}

In $xml you will have whole SOAP XML request.

SoapServer does not have methods for reading SOAP headers.

-- edit: contributed example from manual does not seem to work, header handling method never gets called

dr_bonzo