获取 SoapVar 变量的 XML 作为字符串 - 没有 Web 服务(本地)?

sdb*_*bbs 1 php xml soap

考虑这个简单的代码片段:

<?php
# http://php.net/manual/en/soapvar.soapvar.php

$parm = array();
$parm[] = new SoapVar('123', XSD_STRING, null, null, 'customerNo' );
$parm[] = new SoapVar('THIS', XSD_STRING, null, null, 'selection' );
$parm[] = new SoapVar('THAT', XSD_STRING, null, null, 'selection' );
$out = new SoapVar($parm, SOAP_ENC_OBJECT);

var_dump($out);
?>
Run Code Online (Sandbox Code Playgroud)

在它的来源线程上,据称它会生成类似以下 XML 的内容:

<customerNo>123</customerNo>
<selection>THIS</selection>
<selection>THAT</selection>
Run Code Online (Sandbox Code Playgroud)

...但是,我唯一能看到的var_dump()是这样的:

object(SoapVar)#4 (2) {
  ["enc_type"]=>
  int(301)
  ["enc_value"]=>
  array(3) {
    [0]=>
    object(SoapVar)#1 (3) {
      ["enc_type"]=>
      int(101)
      ["enc_value"]=>
      string(3) "123"
      ["enc_name"]=>
      string(10) "customerNo"
    }
    [1]=> ...
Run Code Online (Sandbox Code Playgroud)

如何在不调用实际远程 Web 服务的情况下获取 SoapVar 对象的预期 XML?

sdb*_*bbs 6

好的,感谢在发送请求之前/不发送请求之前检查由 PHP SoapClient 调用创建的 XML,我想我已经解决了这个问题 - 需要创建一个单独的调试 SoapClient 扩展类,这里是修改后的 OP 代码:

<?php
#<!-- # http://php.net/manual/en/class.soapvar.php -->

class SoapClientDebug extends SoapClient
{
  public function __doRequest($request, $location, $action, $version, $one_way = 0) {
    // Add code to inspect/dissect/debug/adjust the XML given in $request here
    //echo "$request\n"; // OK, but XML string in single line
    $doc = new DomDocument('1.0');
    $doc->preserveWhiteSpace = false;
    $doc->formatOutput = true;
    $doc->loadXML($request);
    $xml_string = $doc->saveXML();
    echo "$xml_string\n";
    // Uncomment the following line, if you actually want to do the request
    // return parent::__doRequest($request, $location, $action, $version, $one_way);
    return ""; # avoids the PHP Fatal error:  Uncaught SoapFault exception: [Client] SoapClient::__doRequest() returned non string value in .../__thisfile__.php:32

  }
}

# http://php.net/manual/en/soapvar.soapvar.php
$parm = array();
$parm[] = new SoapVar('123', XSD_STRING, null, null, 'customerNo' );
$parm[] = new SoapVar('THIS', XSD_STRING, null, null, 'selection' );
$parm[] = new SoapVar('THAT', XSD_STRING, null, null, 'selection' );
$out = new SoapVar($parm, SOAP_ENC_OBJECT);

var_dump($out);

//~ $dbgclient = new SoapClientDebug(NULL); # "URI of the WSDL file or NULL if working in non-WSDL mode." http://php.net/manual/en/soapclient.soapclient.php
$dbgclient = new SoapClientDebug(null, array('location' => "http://localhost/soap.php",
                                              'uri'      => "http://test-uri/"));
$dbgclient->testVar($out);

?>
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://test-uri/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <SOAP-ENV:Body>
    <ns1:testVar>
      <param0 xsi:type="SOAP-ENC:Struct">
        <customerNo xsi:type="xsd:string">123</customerNo>
        <selection xsi:type="xsd:string">THIS</selection>
        <selection xsi:type="xsd:string">THAT</selection>
      </param0>
    </ns1:testVar>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Run Code Online (Sandbox Code Playgroud)

……我想这就是我想要的……