开发者

Can I preview the XML that PHP SOAP wants to send before sending it?

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 i开发者_运维百科t's correct before actually sending it to the SOAP server?


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


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";
?>


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...
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜