SolarBear
SolarBear

Reputation: 4629

PHP SoapClient sends escaped XML characters

I create an XML document using PHP's XMLWriter.

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

Now I connect to a SOAP service in non-WSDL mode.

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

The problem is that the SOAP message received by the SOAP service contains my data as escaped XML characters. (Warning : dummy SOAP message ahead!)

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

The SOAP service will not work with escaped data but, on the other hand, I don't escape my data, the SoapClient does so. How do I make my SoapClient send unescaped data?

Upvotes: 4

Views: 3431

Answers (1)

SolarBear
SolarBear

Reputation: 4629

Escaping characters is the expected behavior of the XmlWriter::writeElement method.

It took me a while to figure it out but the answer was simple : put a SoapVar with the XSD_ANYXML parameter into the SoapParam :

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

The SoapVar documentation mentions that its second parameter (encoding) can be "one of the XSD_... constants" which are not documented in the PHP docs.

Upvotes: 6

Related Questions