代码之家  ›  专栏  ›  技术社区  ›  bcmcfc

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

  •  8
  • bcmcfc  · 技术社区  · 14 年前

    new SoapClient 在尝试运行 __soapCall() 在实际将它发送到SOAP服务器之前确保它是正确的?

    3 回复  |  直到 12 年前
        1
  •  12
  •   VolkerK    14 年前

    您可以使用派生类并覆盖 __doRequest() method

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

        2
  •  6
  •   Josh Andreas Rehm    13 年前

    不是之前,而是之后。见

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

    只有当 SoapClient TRUE .

    <?php
    $client = new SoapClient("some.wsdl", array('trace' => 1));
    $result = $client->SomeFunction();
    echo "REQUEST:\n" . $client->__getLastRequest() . "\n";
    ?>
    
        3
  •  0
  •   shasi kanth skdo    10 年前

    注意,如果您控制了SOAP服务器,那么实际上可以捕获发送到服务器的原始SOAP请求。为此,您需要扩展SOAP服务器。

    示例代码:

    class MySoapServer extends SoapServer 
    {
        public function handle($request = null)
        {
          if (null === $request)    
          $request = file_get_contents('php://input');
          // Log the request or parse it...
        }
    }