Getting the XML as string for a SoapVar variable - without a webservice (locally)?

前端 未结 1 620
礼貌的吻别
礼貌的吻别 2021-01-23 09:41

Consider this simple snippet of code:



        
相关标签:
1条回答
  • 2021-01-23 09:51

    Ok, thanks to Inspect XML created by PHP SoapClient call before/without sending the request, I think I got it solved - one needs to create a separate debug SoapClient extended class, here is the modified OP code with it:

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

    This, at end, will print out:

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

    ... which is what I wanted, I guess...

    0 讨论(0)
提交回复
热议问题