根据标题,是否可以在尝试运行 __soapCall()
之前输出 new SoapClient
创建的 XML,以确保在实际发送之前它是正确的它到 SOAP 服务器?
最佳答案
您可以使用派生类并覆盖 __doRequest() method SoapClient 类。
<?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 '';
}
}
}
打印
<?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
但是您必须稍微更改实际代码才能使其正常工作,我会尽可能避免这种情况(即让工具完成工作)。
关于php - 我可以在发送之前预览 PHP SOAP 要发送的 XML 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4021514/