我可以在发送之前预览PHP SOAP要发送的XML吗?

bcm*_*cfc 8 php soap

根据标题,是否可以new SoapClient在尝试运行之前输出已创建的XML,__soapCall()以确保在将其实际发送到SOAP服务器之前是正确的?

Vol*_*erK 12

您可以使用派生类并覆盖SoapClient类的__doRequest()方法.

<?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 '';
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

版画

<?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
Run Code Online (Sandbox Code Playgroud)

但是你必须稍微改变实际的代码才能使用,我尽量避免使用它(即让工具完成工作).


Gor*_*don 6

不是之前,而是之后.看到

SoapClient::__getLastRequest - 返回上次SOAP请求中发送的XML.

仅当SoapClient在trace选项设置为的情况下创建对象时,此方法才有效TRUE.

手册示例:

<?php
$client = new SoapClient("some.wsdl", array('trace' => 1));
$result = $client->SomeFunction();
echo "REQUEST:\n" . $client->__getLastRequest() . "\n";
?>
Run Code Online (Sandbox Code Playgroud)