Tracing SOAP request and response

suggest change

Sometimes we want to look at what is sent and received in the SOAP request. The body methods below return XML, and the header methods return the HTTP headers. All four stay empty unless the client was constructed with the trace option set to true:

SoapClient::__getLastRequest()
SoapClient::__getLastRequestHeaders()
SoapClient::__getLastResponse()
SoapClient::__getLastResponseHeaders()

For example, suppose we have an ENVIRONMENT constant and when this constant’s value is set to DEVELOPMENT we want to echo all information when the call to getAddress throws an error. One solution could be:

try {
    $address = $soap_client->getAddress(1234);
} catch (SoapFault $e) {
    if (ENVIRONMENT === 'DEVELOPMENT') {
        var_dump(
            $soap_client->__getLastRequestHeaders(),
            $soap_client->__getLastRequest(),
            $soap_client->__getLastResponseHeaders(),
            $soap_client->__getLastResponse()
        );
    }
    // Handle the exception as appropriate for the application.
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents