Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP SoapServer - attributes in nodes

Tags:

php

soap

wsdl

xml

I've seen this question answered elsewhere but I still can't make it work, so I need some further clarification:

The example given was:

$tag['_'] = 'yyy'; 
$tag['attr'] = 'xxx'; 
$tagVar = new SoapVar($tag, SOAP_ENC_OBJECT);

The generated xml would be:

<tag attr="xxx">yyy</tag>

However, I'm getting

<tag>
  <_>yyy</_>
  <attr>xxx</attr>
</tag>

So, is anything else needed to make it work as expected? Some kind of configuration in the SoapServer class or in the WSDL even?

To complicate things a bit more, the element is namespaced, so actually I'm looking for a way to get

<ns:tag attr="xxx">yyy</ns:tag>
like image 902
alepeino Avatar asked May 26 '16 17:05

alepeino


1 Answers

The PHP soap function are so mad and I've never found out what was so wrong in it. I was trying to connect and update data into zimbra via SOAP API, and had many issues cuz of it. So I used SimpleXMLElement & Curl :)

There you can build your XML like this :

$xml = new SimpleXMLElement('<soap></soap>'); // create your base

$xml = $xml->addChild('tag', str_replace('&', '&amp;', 'yyy')); // see addChild in docs
$xml->attr = 'xxx'; // escaping content rather than addAttribute which does not

echo $xml->asXML(); // which returns : <tag>yyy<attr>xxx</attr></tag>

For the namespace, there is a namespace argument in addChild, but this does not output what you want ...

$xml = $xml->addChild('tag', str_replace('&', '&amp;', 'yyy'), 'ns');
$xml->attr = 'xxx';

echo $xml->asXML(); // which returns : <tag>yyy<attr>xxx</attr></tag>

PS : if you are running in browser, dont forget to htmlspecialchars the echos :)

like image 61
Bobot Avatar answered Oct 20 '22 19:10

Bobot