tags:

views:

350

answers:

4

Does anyone know how to log all request and responses with the builtin SoapClient in PHP? I could in fact manually log everything with SoapClient::__getLastRequest() and SoapClient::__getLastResponse() But we have that much soap requests in our system that i'm looking other possibilities.

Note: i'm using wsdl mode so using a method that tunnels all through to SoapClient::__soapCall() isn't an option

+1  A: 

Would something like this work?

class MySoapClient extends SoapClient
{
    function __soapCall($function_name, $arguments, $options = null, $input_headers = null, &$output_headers = null) 
    {
        $out = parent::__soapCall($function_name, $arguments, $options, $input_headers, $output_headers);

        // log request here...
        // log response here...

        return $out;
    }
}

Since SoapClient already sends all requests through __soapCall, you can intercept them by subclassing SoapClient and overriding it. Of course, to make it work you need to also replace every new SoapClient(...) in your code with new MySoapClient(...), but that seems like a pretty easy search and replace task.

Aleksander Kmetec
+1  A: 

I think the better way is to override SoapClient::__doRequest() (and not SoapClient::__soapCall()) as you'll have direct access to the request- as well as to the response-XML. But the general approach to subclass SoapClient should be the way to go.

class My_LoggingSoapClient extends SoapClient
{
    // logging methods

    function __doRequest($request, $location, $action, $version, $one_way = 0) 
    {
        $this->_logRequest($location, $action, $version, $request);
        $response = parent::__doRequest($request, $location, $action, $version, $one_way);
        $this->_logResponse($location, $action, $version, $response);
        return $response;
    }
}

EDIT

From an OOP-design / design pattern point of view a Decorator is obviously the better way to handle this kind of problem - please see Gordon's answer. But this is a little bit more difficult to implement.

Stefan Gehrig
+2  A: 

I second Aleksanders and Stefans suggestion but would not subclass SoapClient. Instead I'd wrap the regular SoapClient in a decorator, because logging is not a direct concern of the SoapClient. In addition, the loose coupling lets you easily substitute the SoapClient with a mock in your UnitTests, so you can concentrate on testing the logging functionality. If you only want to log specific calls, you can add some logic that filters requests and responses by $action or anything you see fit.

Edit since Stefan suggested to add some code, the decorator would probably look something like this, although I am not sure about the __call() method (see Stefans comments)

class SoapClientLogger
{
    protected $soapClient;

    // wrapping the SoapClient instance with the decorator
    public function __construct(SoapClient $client)
    {
        $this->soapClient = $client;
    }

    // Overloading __doRequest with your logging code
    function __doRequest($request, $location, $action, $version, $one_way = 0) 
    {
         $this->log($request, $location, $action, $version);

         $response = $this->soapClient->__doRequest($request, $location, 
                                                    $action, $version, 
                                                    $one_way);

         $this->log($response, $location, $action, $version);
         return $response;
    }

    public function log($request, $location, $action, $version)
    {
        // here you could add filterings to log only items, e.g.
        if($action === 'foo') {
            // code to log item
        }
    }

    // route all other method calls directly to soapClient
    public function __call($method, $args)
    {
        // you could also add method_exists check here
        return call_user_func_array(array($this->soapClient, $method), $args);
    }
}
Gordon
Using a decorator is a very good idea. Actually I'd go with a decorator-solution myself if I had to work on the same problem. But I think, the subclassing-solution is a little bit more comprehensible.
Stefan Gehrig
Perhaps it'll help the OP if you add a code sample.
Stefan Gehrig
And then there is a PHP problem that disallows the *stacking* of decorators if you're using the method-overloading-feature `__call()` in one of your decorators or in the class to be decorated (`SoapClient` in this case).
Stefan Gehrig
I didnt know there was an issue with __call(). Do you have a link where I can read about it? This would be good to know. Oh, and I added code above.
Gordon
What I meant is that you cannot determine if a method is callable when using `__call()`, so calling a non-existing method will always raise a fatal error. In case of decorating the `SoapClient` you could check `is_callable(array($this->soapClient, $method))` and `in_array($method, $this->soapClient-> __getFunctions())`. But this will not allow to stack decorators.
Stefan Gehrig
Forget my remark about the problem (and the stacking of decorators)... It is possible to do the checks I showed above and still be able to stack your decorators. Sorry for all the chaos *g*
Stefan Gehrig
A: 

Sorry to revisit such an old post, but I encountered some challenges with the accepted answer's implementation of a decorator that is responsible for the logging of the Soap requests and wanted to share in case anyone else runs into this.

Imagine you set up your instance like the following, using the SoapClientLogger class outlined in the accepted answer.

$mySoapClient = new SoapClientLogger(new SoapClient());

Presumably any method you call on the SoapClientLogger instance will get passed through the __call() method and executed on the SoapClient. However, typically you make use of a SoapClient by calling the methods generated from the WSDL, like this:

$mySoapClient->AddMember($parameters); // AddMember is defined in the WSDL

This usage will never hit the SoapClientLogger's _doRequest() method and thus the request will not be logged. Instead AddMember() is routed through $mySoapClient::_call() and then right on down to the SoapClient instance's _doRequest method.

I'm still searching for an elegant solution to this.

stereointeractive.com