Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a SoapVar containing CDATA with XML?

I have to call a SOAP web service using a SoapClient object. One of the parameters has to contain some XML included in a CDATA section, like this:

<ns2:productInformation><![CDATA[<foo><bar>baz</bar></foo>]]></ns2:productInformation>

Note the node namespace, it's the source of my pain...

If I create a string SoapVar, the XML is encoded...

new SoapVar('<![CDATA[<foo><bar>baz</bar></foo>]]>', XSD_STRING, null, null, 'productInformation', self::MY_NAMESPACE)

<ns2:productInformation xsi:type="xsd:string">&lt;![CDATA[&lt;foo&t;&lt;bar&gt;baz&lt;/bar&gt;&lt;/foo&gt;]]&gt;</ns2:productInformation>

So I can't do this way. The only alternative I've found is to use the XSD_ANYXML encoding, like this:

new SoapVar('<ns2:productInformation><![CDATA[<foo><bar>baz</bar></foo>]]></ns2:productInformation>', XSD_ANYXML)

It works, but it's bad... Look at the hard coded namespace shortcut ("ns2").

So do you have an idea to create a CDATA section containing XML?

like image 340
Gregoire Avatar asked Jun 13 '12 13:06

Gregoire


1 Answers

I've had the same requirement (maldesigned vendor web service that requires embedding of part of the request as XML encoded in a string, and which blows up if you give it an entity-encoded string instead of CDATA).

To the best of my ability to determine, your SoapVar workaround is as good as it gets. Sorry. I'm pretty unhappy about having to hardcode a namespace reference myself.

like image 174
chaos Avatar answered Nov 01 '22 11:11

chaos