PHP SoapClient发送转义的XML字符

时间:2014-06-26 13:14:10

标签: php xml soap

我使用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发送未转义的数据?

1 个答案:

答案 0 :(得分:5)

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

我花了一段时间来弄明白但答案很简单:将SoapVar XSD_ANYXML参数放入SoapParam

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

The SoapVar documentation提到它的第二个参数(编码)可以是“ XSD _...常量之一”,这些都没有在PHP文档中记录。