ChatGPT解决这个技术问题 Extra ChatGPT

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

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.


s
shamittomar

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));

Still useful after 6 years
Similarly, if you want to get the response XML, use SoapClient::__getLastResponse(). See this.
@KasunRajapaksha still useful after (nearly) 12 years :)
k
kmoser
$SOAP = new SoapClient($WSDL, array('trace' => true));

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

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

This will not only print the last request but also make the XML tags visible in the browser.


Yeah, this my GOTCHA! I foolishly forgot that Chrome simply drops all the XML it finds, so it didn't matter that a
 was wrapping the response and thought that SoapClient was refusing to display the last request. A simple view:source corrected that and htmlspecialchars() made it all better.
                        				                   		
Q
Quinn Comendant

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);

T
TorranceScott

Extending Quinn's answer, you can also just log the request before you perform the request.

class SoapClientDebug extends SoapClient
{

public function __doRequest($request, $location, $action, $version, $one_way = 0)
{
    error_log("REQUEST:\n" .$request . "\n");
    error_log("LOCATION:\n" .$location . "\n");
    error_log("ACTION:\n" .$action . "\n");
    error_log("VERSION:\n" .$version . "\n");
    error_log("ONE WAY:\n" .$one_way . "\n");

    return parent::__doRequest($request, $location, $action, $version, $one_way);
}
}

E
Ezequiel Muns

You need to enable tracing when you create your SoapClient. Like so:

$SOAP = new SoapClient($WSDL, array('trace' => true));

$Data = array('Something'=>'Some String','SomeNumber'=>22); 

Then call the __getLastRequest method after you've made a service call to see the XML.

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

This will output the request XML.

More reading: http://www.php.net/manual/en/soapclient.getlastrequest.php


I have tried to get XML values, my code: justpaste.it/3rwzs and return values as an object, how can i get my values as XML? thank you @Ezequiel Muns
@Gem you can use echo htmlentities($SOAP->__getLastRequest()); to see it as XML
P
Pratik Bhatt

if you are running the client locally, Fiddler is a great implementation agnostic way of looking at the messages on the wire.

If you are running it remotely then you could use something like Apache TCPMON Standalone or through eclipse*

*just linking to the first hit from Google


Fiddler is useless if SOAP service is behind https as php_openssl.dll cannot be traced as it's not using wininet.
C
Community

The problem with Quinn Comendant's answer, that $request from __doRequest() will then be processed by __call() and the user will see an array of parameters instead of real xml request. To prevent this, such workaround can be used:

class DummySoapClient extends SoapClient {
    function __construct($wsdl, $options) {
        parent::__construct($wsdl, $options);
    }

    function __doRequest($request, $location, $action, $version, $one_way = 0) {
        throw new Exception($request);
    }

    function __call($function_name, $arguments)
    {
        try {
            parent::__call($function_name, $arguments);
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }
}

Option trace is not necessary here, because we do not call __getLastRequest() or other relevant functions.