How do I see the actual XML generated by PHP SOAP Client Class?

How do I see the actual XML generated by PHP SOAP Client Class?

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

Stefan Bogdanescu

Founder & Senior Architect

Inspecting the Raw XML from PHP's SoapClient

When debugging a SOAP integration, you often need to see exactly what payload is being sent over the wire before it hits the server. The standard SoapClient class abstracts this process away by default. It takes your associative array, consults the WSDL to map types and namespaces, constructs the SOAP envelope, signs headers if required, and sends the HTTP request. While convenient for production code, this abstraction makes it difficult to verify that the generated XML matches the remote service's expectations during troubleshooting.

To inspect the raw XML generated by SoapClient, you must intercept the execution flow before the SoapClient method is called. The most direct approach involves leveraging PHP's built-in debugging capabilities or wrapping the call in a custom proxy class.

Intercepting via SoapClient Options

One of the simplest ways to expose the generated XML without writing a wrapper class is to use the options parameter when instantiating the client. This array allows you to configure specific behaviors, including enabling debug logging.

You can pass an options array with the debug_wsdl flag set to true. While this primarily triggers WSDL caching logs, it often provides insight into the parsing of the service definition. However, for seeing the outgoing request XML specifically, the most reliable method within the native class is to enable the trace option.

$SOAP = new SoapClient($WDSL, [
    'trace' => true,
]);

// Build an array of data to send in the request.
$Data = array('Something' => 'Some String', 'SomeNumber' => 22); 

$Response = $SOAP->DoRemoteFunction($Data);

When trace is enabled, PHP attempts to log the SOAP headers and body information. The location of these logs depends on your PHP configuration (php.ini) environment variables like soap.wsdl_cache_enabled, and whether you are running in CLI or a web server context. In many environments, this output appears in the standard error stream or specific system logs rather than directly within the script's output buffer.

Implementing a Custom SoapClient Proxy

If relying on configuration flags yields inconsistent results across different hosting environments, creating a custom proxy class is a more robust solution. This approach guarantees that you capture the XML string regardless of server settings.

The strategy involves extending SoapClient or, more cleanly in modern PHP 8+ contexts, utilizing a closure to intercept the call. Since SoapClient methods are generated dynamically based on the WSDL, you cannot easily override individual method signatures without regenerating the class. Instead, the best practice is to wrap the call using a custom wrapper that serializes the arguments before they reach the client.

Here is a pattern using a closure to inspect the request:

function createTracingClient($wsdl) {
    $options = [
        'trace' => true,
        'cache_wsdl' => WSDL_CACHE_NONE, // Prevent caching issues during debugging
    ];

    return new SoapClient($wsdl, $options);
}

$SOAP = createTracingClient($WDSL);

// Wrap the call to inspect arguments before sending
$Data = array('Something' => 'Some String', 'SomeNumber' => 22); 

// You can use a custom class extending SoapClient if you need to override specific methods,
// but often simply logging the $Data variable against the WSDL schema is sufficient 
// for manual inspection if automatic XML logging is not available.

$Response = $SOAP->DoRemoteFunction($Data);

For strict inspection of the generated envelope string itself, the most effective programmatic approach in PHP 8+ is to use a custom class that extends SoapClient and overrides the method resolution logic, or to utilize the underlying SOAP stack features available via soapvar() in older versions, though modern best practices favor intercepting at the HTTP level.

Debugging at the HTTP Level

If the native PHP logging mechanisms are insufficient or behave unpredictably, switching your debugging strategy to the HTTP layer is often superior. Since SoapClient ultimately issues an HTTP POST request with a text/xml content type, tools like curl or browser developer networks can capture the exact bytes transmitted.

Constructing the curl command manually allows you to see the full envelope, including any namespace prefixes and encoding quirks that might be invisible in high-level logs.

curl -v "https://your-soap-endpoint" \
  --header "Content-Type: text/xml; charset=utf-8" \
  --data-binary @request.xml

This method bypasses the SoapClient abstraction entirely, giving you an unfiltered view of the network traffic. It is particularly useful when troubleshooting issues related to character encoding or unexpected namespace declarations that the client library might be altering automatically.

Trade-offs and Considerations

Using trace options can impact performance if enabled in production, as it may generate verbose logs that clutter error streams. Furthermore, relying on server-side configuration flags for debugging is fragile; what works locally might fail on a shared hosting environment where you cannot modify php.ini. A custom wrapper or HTTP-level inspection provides deterministic behavior but requires more code maintenance. Always ensure that any logging mechanism adheres to your security policies, especially if the XML contains sensitive data before it is stripped by the remote service.

Our network