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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With