SOAP headers and PHP 5.2.3

My last post about how to handle SOAP headers was done with PHP 5.3.5-1ubuntu7.7 with Suhosin-Patch (cli) (built: Feb 11 2012 06:22:51).

I tried to run the same installation on PHP 5.2.3 and it failed to handle SOAP headers.
Documentation on php.net is a bit lacking of details about this. Even after an extended search with friend google, I’m still not sure if it is some flaw in the installation or if PHP 5.2.3 simply cannot yet handle the SOAP headers.

But well, there is always a (dirty) hack and at least this one points a way out if you might be stuck using older PHP versions. And I learned a lot about DOM document as well 😀

So what we can do in this case, is to extend the PHP SoapServer class and add our own handling function. The standard SoapServer::handle function is usually called without any arguments. In this case it is assumed, that the SOAP request data can be found in $HTTP_RAW_POST_DATA (php.ini setting always_populate_raw_post_data must be set to true for this) or by reading php://input.
So what we do is init a SOAP client and set the SOAP object in our own handling function

For this we simply extend class SoapServer with out own class, grab the request data before calling handle, parse request for authHeader, make a new SOAP object and use the extracted info for user authentication or whatever we want to do before the real handle starts (call to hrapp_soap_soapclass::checkParams). Then we set the SOAP object to SOAP server and handle the request.

By doing so we could even completely change the request or decide on the fly about using a completely different soap object class etc… Think about other uses of an extended SoapServer class, not only for forcing SOAP header handling on PHP 😉

Here’s the code:

SOAP request with a SOAP header

SOAP request with a SOAP header

SOAP server

SOAP server

SOAP server, header parser

SOAP server, header parser

SOAP class

SOAP class

This entry was posted in howto, PHP, SOAP. Bookmark the permalink.

Comments are closed.