As per the title, is it possible to output the XML that a new SoapClient
has created before trying to run a __soapCall()
to ensure it's correct before actually sending it to the SOAP server?
Asked
Active
Viewed 7,780 times
8
-
Do you want to do this as part of your script or "only" as a debugging tool (so it would be better not to change the script at all but to inspect the object with tools) – VolkerK Oct 26 '10 at 07:39
3 Answers
13
You could use a derived class and overwrite the __doRequest() method of the SoapClient class.
<?php
//$clientClass = 'SoapClient';
$clientClass = 'DebugSoapClient';
$client = new $clientClass('http://www.webservicex.com/CurrencyConvertor.asmx?wsdl');
$client->sendRequest = false;
$client->printRequest = true;
$client->formatXML = true;
$res = $client->ConversionRate( array('FromCurrency'=>'USD', 'ToCurrency'=>'EUR') );
var_dump($res);
class DebugSoapClient extends SoapClient {
public $sendRequest = true;
public $printRequest = false;
public $formatXML = false;
public function __doRequest($request, $location, $action, $version, $one_way=0) {
if ( $this->printRequest ) {
if ( !$this->formatXML ) {
$out = $request;
}
else {
$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
$doc->loadxml($request);
$doc->formatOutput = true;
$out = $doc->savexml();
}
echo $out;
}
if ( $this->sendRequest ) {
return parent::__doRequest($request, $location, $action, $version, $one_way);
}
else {
return '';
}
}
}
prints
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.webserviceX.NET/">
<SOAP-ENV:Body>
<ns1:ConversionRate>
<ns1:FromCurrency>USD</ns1:FromCurrency>
<ns1:ToCurrency>EUR</ns1:ToCurrency>
</ns1:ConversionRate>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
NULL
But you'd have to change the actual code a bit for this to work which I try to avoid when possible (i.e. let tools do the work).

VolkerK
- 95,432
- 20
- 163
- 226
-
1It's probably ok as long as you're in the prototype/evaluation phase. Later you should try "non-invasive" tools. Haven't tried it with php/sockets but fiddler and wireshark can probably do the job. Either let them fetch the network packets or configure your script to use them as a proxy. http://www.fiddler2.com/fiddler2/ – VolkerK Oct 26 '10 at 10:05
-
That's exactly it - I just wanted to ensure the XML was correct in the initial setup phase. – bcmcfc Oct 26 '10 at 11:04
6
Not before, but after. See
SoapClient::__getLastRequest
- Returns the XML sent in the last SOAP request.
This method works only if the SoapClient
object was created with the trace option set to TRUE
.
Example from manual:
<?php
$client = new SoapClient("some.wsdl", array('trace' => 1));
$result = $client->SomeFunction();
echo "REQUEST:\n" . $client->__getLastRequest() . "\n";
?>
-
I'm using that at the minute, I just wanted to know if there was of seeing the XML before it was sent. I guess that answers the question though - there isn't! – bcmcfc Oct 26 '10 at 07:54
0
As a note, if you have control of the SOAP Server, you can actually catch the original SOAP request that is sent to the server. For this, you need to extend SOAP Server.
A sample code:
class MySoapServer extends SoapServer
{
public function handle($request = null)
{
if (null === $request)
$request = file_get_contents('php://input');
// Log the request or parse it...
}
}

shasi kanth
- 6,987
- 24
- 106
- 158