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

05/21/2020 23:00:02

Consider this example SOAP Client script:

$SOAP = new SoapClient($WDSL); // Create a SOAP Client from a WSDL

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

$Response = $SOAP->DoRemoteFunction($Data); // Send the request.

On the last line, PHP takes the arguments from the array you specified, and, using the WSDL, builds the XML request to send, then sends it.

How can I get PHP to show me the actual XML it's built?

I'm troubleshooting an application and need to see the actual XML of the request.

Verified Answer (112 Votes)

08/26/2010 11:08:31

Use getLastRequest. It returns the XML sent in the last SOAP request.

echo "REQUEST:\n" . $SOAP->__getLastRequest() . "\n";

And remember, this method works only if the SoapClient object was created with the trace option set to TRUE. Therefore, when creating the object, use this code:

$SOAP = new SoapClient($WDSL, array('trace' => 1));
112

Answer #2 (19 Votes)

10/12/2010 01:12:25
$SOAP = new SoapClient($WSDL, array('trace' => true));

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

echo "REQUEST:\n" . htmlentities($SOAP->__getLastRequest()) . "\n";

this will not print the last request but also make the xml tags visible in the browser

19

Answer #3 (15 Votes)

04/08/2014 00:36:31

If you'd like to view the request without actually making a connection, you can override SoapClient's __doRequest method to return the XML:

class DummySoapClient extends SoapClient {
    function __construct($wsdl, $options) {
        parent::__construct($wsdl, $options);
    }
    function __doRequest($request, $location, $action, $version, $one_way = 0) {
        return $request;
    }
}
$SOAP = new DummySoapClient('http://example.com/?wsdl', array('trace' => true));
echo $SOAP->GetRequestDetail($params);
15
3
Hack Hex uses Stack Exchance API by the Stack Exchange Inc. to scrape questions/answers under Creative Commons license.