【问题标题】:PHP SoapClient sends escaped XML charactersPHP SoapClient 发送转义的 XML 字符
【发布时间】:2014-08-17 08:48:58
【问题描述】:

我使用 PHP 的 XMLWriter 创建了一个 XML 文档。

    $xmlWriter = new \XMLWriter();
    $xmlWriter->openMemory(); //generate XML in-memory, not on disk

    //Please keep the indentation intact to preserve everybody's sanity!
    $xmlWriter->startElement('RootElement');
    // ...
    $xmlWriter->endElement();

    $myXml = $xmlWriter->outputMemory(true);

现在我以非 WSDL 模式连接到 SOAP 服务。

        $soapClient = new \SoapClient(null, array(
            "location" => "https://theservice.com:1234/soap/",
            "uri" =>  "http://www.namespace.com",
            "trace" => 1
            )
        );

        $params = array(
                new \SoapParam($myXml, 'param')
            );

        $result = $soapClient->__soapCall('method', $params);

问题是 SOAP 服务接收到的 SOAP 消息包含我的数据作为转义的 XML 字符。 (警告:前面的虚拟 SOAP 消息!)

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope" ...>
    <SOAP-ENV:Header>
        ...
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <ns1:method>
            <Request xsi:type="xsd:string">
                &lt;Root&gt;
                    (escaped data)
                &lt;/Root&gt;
            </Request>
        </ns1:method>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

SOAP 服务不能处理转义的数据,但另一方面,我不会转义我的数据,SoapClient 会这样做。如何让我的SoapClient 发送未转义的数据?

【问题讨论】:

    标签: php xml soap


    【解决方案1】:

    转义字符is the expected behavior of the XmlWriter::writeElement method

    我花了一段时间才弄明白,但答案很简单:将带有 XSD_ANYXML 参数的 SoapVar 放入 SoapParam 中:

    $params = array(
        new \SoapParam(new \SoapVar($myXml, XSD_ANYXML), 'param')
    );
    

    The SoapVar documentation 提到它的第二个参数(编码)可以是 PHP 文档中未记录的“XSD_...常量之一”。

    【讨论】:

      猜你喜欢
      • 2012-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-11
      • 2015-06-23
      相关资源
      最近更新 更多