0

I need to create an xml to send via soap, but I can not. I need an xml with a CDATA tag and I can not do it. The xml must be in this format:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="url-here">
   <soapenv:Header/>
   <soapenv:Body>
      <ser:callServiceMethod>
         <!--Optional:-->
         <xml>
         <![CDATA[<fruits>
                    <type>G</type>
                    <flav>grape</flav>
                    <orders>
                        <order>
                            <client>12345</client>
                            <itens>
                                <item>
                                    <cod>1</cod>
                                    <name>Grape</name>
                                </item>
                                <item>
                                    <cod>2</cod>
                                    <name>Apple</name>
                                </item>
                            </itens>
                        </order>
                    </orders>
                </fruits>]]>
        </xml>
      </ser:callServiceMethod>
   </soapenv:Body>
</soapenv:Envelope>

I tried to create an array to generate the xml, this works, but without the CDATA, like this:

$soapArgs = array(
    'xml' => array(
    //need CDATA here
        'fruits' => array(
            'type' => 'G',
            'flav' => 'grape',
            'orders' => array(
                'order' => array(
                    'client' => 12345,
                    'itens' => array(
                        'item' => array(
                            'cod' => 1,
                            'name' => 'Grape',
                        ),
                        'item' => array(
                            'cod' => 2,
                            'name' => 'Apple',
                        ),
                    )
                )
            )
        )
    )
);

$soapClient = new SoapClient($params);
$serviceResponse = $soapClient->callServiceMethod($soapArgs);

How do I generate this xml with CDATA to be able to send via SOAP?

Thank you guys!

Junior Maia
  • 17
  • 1
  • 12

1 Answers1

1

As discussed under What does <![CDATA[]]> in XML mean? a CDATA section is a way of including a string in an XML document without it being parsed. Everything from <fruits> to </fruits> is therefore not part of the actual XML document; as far as the SOAP request is concerned, it's just a string.

(This kind of thing is quite common in SOAP services, because people find it easier to parse the payload outside of their SOAP code, so they use SOAP as a large wrapper around arbitrary content. It could just as well be <json><![CDATA[{"foo": "bar"}]]></json> or some other format of payload.)

Assuming the owner of the SOAP service hasn't done something really weird, this:

   <xml>
     <![CDATA[<fruits>
                <type>G</type>
                <flav>grape</flav>
                <orders>
                    <order>
                        <client>12345</client>
                        <itens>
                            <item>
                                <cod>1</cod>
                                <name>Grape</name>
                            </item>
                            <item>
                                <cod>2</cod>
                                <name>Apple</name>
                            </item>
                        </itens>
                    </order>
                </orders>
            </fruits>]]>
   </xml>

should be completely equivalent to this:

   <xml>
           &lt;fruits&gt;
                &lt;type&gt;G&lt;/type&gt;
                &lt;flav&gt;grape&lt;/flav&gt;
                &lt;orders&gt;
                    &lt;order&gt;
                        &lt;client&gt;12345&lt;/client&gt;
                        &lt;itens&gt;
                            &lt;item&gt;
                                &lt;cod&gt;1&lt;/cod&gt;
                                &lt;name&gt;Grape&lt;/name&gt;
                            &lt;/item&gt;
                            &lt;item&gt;
                                &lt;cod&gt;2&lt;/cod&gt;
                                &lt;name&gt;Apple&lt;/name&gt;
                            &lt;/item&gt;
                        &lt;/itens&gt;
                    &lt;/order&gt;
                &lt;/orders&gt;
            &lt;/fruits&gt;
   </xml>

The function creating the XML will handle escaping the string, or wrapping it in CDATA, so all you should need is:

$xml = '<fruits>
                <type>G</type>
                <flav>grape</flav>
                <orders>
                    <order>
                        <client>12345</client>
                        <itens>
                            <item>
                                <cod>1</cod>
                                <name>Grape</name>
                            </item>
                            <item>
                                <cod>2</cod>
                                <name>Apple</name>
                            </item>
                        </itens>
                    </order>
                </orders>
            </fruits>';
$soapArgs = array('xml' => $xml);

How you create the string $xml is, obviously, up to you.

IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • Nice! But in that case you literally wrote the xml in a string, how do I convert my array to a string like yours, or somehow that array becomes an xml in the send? Because I find it more elegant to create as an array and not as a direct string .. – Junior Maia Jul 05 '18 at 17:03
  • @JuniorMaia You just need a separate function to create the XML you want, however you want to specify it. If you want to specify it as an array, just search for "PHP array to XML" and you'll find plenty of examples of how to do it which would work for a simple case like this. – IMSoP Jul 05 '18 at 17:17
  • Can you give me an example, please? – Junior Maia Jul 05 '18 at 17:32
  • 1
    @JuniorMaia https://duckduckgo.com/?q=php+%22array+to+xml%22&t=ffsb&ia=qa – IMSoP Jul 05 '18 at 17:38