How to dump SoapClient request or response for debug?

How to dump SoapClient request or response for debug?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Debugging SoapClient When Reflection Methods Return Null

Encountering NULL returns from $client->__getLastRequest() or $client->__getLastResponse() is a classic frustration for anyone working with PHP's SOAP extension. It feels like the debugging hooks are broken, but the reality lies in the lifecycle of the SoapClient object and when those internal buffers are actually populated.

The reflection methods __getLastRequestHeaders(), __getLastRequest(), and __getLastResponse() do not store data by default. They only return the SOAP envelope or headers associated with the most recent operation that has successfully completed a round trip. In your specific scenario, the code returns NULL for the request and response headers because those methods are invoked before the internal state is set up correctly relative to the execution flow, or more likely in modern environments, the default SoapClient instance does not automatically capture these logs unless explicitly configured with debug settings.

Enabling Debug Logging Properly

The most reliable way to inspect raw SOAP traffic without relying on deprecated reflection methods is to instantiate the client with the DEBUG_WSDL or DEBUG flags enabled via the constructor options. This forces the extension to capture the outgoing XML and incoming headers into accessible properties immediately after an operation completes.

Here is how you should configure the client:

$options = [
    'trace' => 1, // Enable tracing to get request/response data
];

$client = new SoapClient("https://example.com/service.wsdl", $options);

$response = $client->DoFunction([
    "Parameter1" => 100,
]);

// Now these will return the actual XML strings
$requestXml = $client->__getLastRequest();
$responseHeaders = $client->__getLastResponseHeaders(); // Note: returns headers, not body

By setting the second argument to an array containing 'trace' => 1, you instruct the SOAP extension to buffer the request and response. If you attempt to call __getLastRequest() without enabling tracing first, or if you are using a version of PHP where these methods were deprecated in favor of WSDL caching behaviors, you will get NULL.

Understanding the Response Object

In your provided example, the $response object successfully contains the FunctionResult float value. This indicates that the SOAP body was parsed correctly by the default deserializer. However, when debugging complex types or checking for SOAP faults, accessing the raw XML is often necessary to see whitespace normalization issues or schema errors that are stripped away during parsing.

The reflection methods you attempted to use are legacy features. In older PHP versions, calling them without trace enabled resulted in empty strings. In newer versions, these methods may have been removed entirely or behave differently depending on the SOAP extension build. Relying on var_dump($client->__getLastRequest()) is safe only if tracing was explicitly turned on during instantiation.

Alternative: Logging via Stream Wrappers

If you cannot modify the client options or are dealing with a legacy codebase that doesn't support the trace option, you can intercept the traffic at the network level. By configuring PHP to log all HTTP/HTTPS traffic to a specific file before sending it to the remote server, you can capture the raw SOAP envelopes regardless of the SoapClient configuration.

This is useful when the internal buffers are inaccessible or cleared prematurely. You can set this up in php.ini:

http_wrapper = /path/to/debug.log
https_wrapper = /path/to/debug.log

Or programmatically before creating the client:

$stream_context = stream_context_create([
    'ssl' => [
        'verify_peer' => false, // Be careful with production usage
        'verify_peer_name' => false
    ],
    // Custom headers or options can be added here if needed
]);

// Note: This approach depends on the specific PHP build and stream wrapper support.

However, this method is less precise for SOAP-specific debugging because it mixes generic HTTP traffic with SOAP envelopes. It captures headers like SOAPAction and Content-Type, which are crucial for understanding how the server interprets your call.

When Tracing Fails: Using a Proxy

Sometimes the issue isn't the client configuration but the environment itself. If you are running in an environment where WSDL caching is aggressive or if the SOAP extension is compiled without full debugging support, you might see NULL despite enabling trace options. In such cases, routing your traffic through a local proxy like mitmproxy or using tcpdump to sniff network packets provides a definitive view of the exchange.

For example, running tcpdump -i any port 443 -w soap_trace.pcap allows you to inspect the encrypted (or unencrypted if using localhost) traffic directly. This bypasses the PHP API entirely and confirms whether the server is actually receiving the request or if the issue lies in the connection layer.

Best Practices for Production Debugging

While enabling trace => 1 is excellent for local development, it should generally be disabled in production environments. Logging large SOAP envelopes to standard output can slow down high-throughput services and expose sensitive data if not handled carefully. If you must inspect traffic in a live environment, consider creating a temporary debug instance of the client specifically for troubleshooting, rather than modifying the primary service object.

If you are migrating away from raw SoapClient usage, frameworks like Laravel provide built-in debugging tools that can log Guzzle or other HTTP clients transparently. For more modern SOAP interactions, switching to libraries that offer better visibility into the request lifecycle is often a smoother path than relying on legacy reflection methods.

Our network