tags:

views:

31

answers:

2

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?

+2  A: 

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";
?>
Gordon
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
+3  A: 

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/"&gt;
  <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
Brilliant, thanks.
bcmcfc
It'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
That's exactly it - I just wanted to ensure the XML was correct in the initial setup phase.
bcmcfc