0

I am trying to create a SOAP request for a paypal express checkout with SimpleXML. However, I am experiencing a behaviour I don't yet understand.

The envelope and its header are generated this way:

$envelope = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:ebay:apis:eBLBaseComponents" xmlns:ns2="urn:ebay:api:PayPalAPI" /> '); $header = $envelope->addChild('SOAP-ENV:Header'); $requesterCredentials = $header->addChild('ns2:RequesterCredentials'); $credentials = $requesterCredentials->addChild('ns1:Credentials'); $credentials->addChild('ns1:Username', 'foo'); $credentials->addChild('ns1:Password', 'bar'); 

Which yields the following output:

<?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:ebay:apis:eBLBaseComponents" xmlns:ns2="urn:ebay:api:PayPalAPI"> <SOAP-ENV:Header> <SOAP-ENV:RequesterCredentials> <SOAP-ENV:Credentials> <SOAP-ENV:Username>foo</SOAP-ENV:Username> <SOAP-ENV:Password>bar</SOAP-ENV:Password> </SOAP-ENV:Credentials> </SOAP-ENV:RequesterCredentials> </SOAP-ENV:Header> </SOAP-ENV:Envelope> 

Every node is now prefixed with SOAP-ENV, which is not what I want. Only the root node and header should be prefixed with SOAP-ENV, the other tags should get the defined namespace prepended in addChild().

The desired output should be:

<?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:ebay:apis:eBLBaseComponents" xmlns:ns2="urn:ebay:api:PayPalAPI"> <SOAP-ENV:Header> <ns2:RequesterCredentials> <ns1:Credentials> <ns1:Username>foo</ns1:Username> <ns1:Password>bar</ns1:Password> </ns1:Credentials> </ns2:RequesterCredentials> </SOAP-ENV:Header> </SOAP-ENV:Envelope> 

What am I doing wrong here?

1
  • 2
    To explain what happens, SimpleXML uses the namespace of the element as a default namespace. You append a new element node without the defining the namespace, SimpleXML uses its default namespace. The prefixes get optimized so it gets unified to the SOAP-ENV prefix. Commented Mar 12, 2015 at 10:59

1 Answer 1

2

addChild takes the namespace as the third parameter:

$requesterCredentials = $header->addChild('RequesterCredentials', null, 'urn:ebay:api:PayPalAPI'); $credentials = $requesterCredentials->addChild('Credentials', null, 'urn:ebay:apis:eBLBaseComponents'); $credentials->addChild('Username', 'foo', 'urn:ebay:apis:eBLBaseComponents'); $credentials->addChild('Password', 'bar', 'urn:ebay:apis:eBLBaseComponents'); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.