7

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>

1 Answer 1

2
+50

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

Sign up to request clarification or add additional context in comments.

4 Comments

Yeah, addAttribute does help. I was hoping for some higher-level solution though.
You mean giving an array to a function and let it making the stuff ?
Then you can use FluidXML to get this higher :) github.com/servo-php/fluidxml
Thanks for the reference to that library, it seems useful. However I'm already using the basic functionality with the automatic array to xml conversion (namespacing included) that the SoapServer takes care of. For nodes that need attributes, I'm trying some weird SoapVar thing... Anyway, +1 and +bounty for the effort. Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.