3

I haven't done any xml projects, so I'm not quite sure what to do with this data...

I'm using curl to make a request to salesforce, and they give me back a response that I need to parse. I want to use simplexml. Here's part of the response:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <loginResponse> <result> <metadataServerUrl> https://na6-api.salesforce.com/services/Soap/m/18.0/ </metadataServerUrl> <passwordExpired> false </passwordExpired> <sandbox> false </sandbox> <serverUrl> https://na6-api.salesforce.com/services/Soap/u/18.0/ </serverUrl> <sessionId> !AQ4AQLtDIqY. </sessionId> <userId> </userId> <userInfo> <accessibilityMode> false </accessibilityMode> <currencySymbol> $ </currencySymbol> <orgDefaultCurrencyIsoCode> USD </orgDefaultCurrencyIsoCode> <orgDisallowHtmlAttachments> false </orgDisallowHtmlAttachments> <orgHasPersonAccounts> false </orgHasPersonAccounts> <organizationId> </organizationId> <organizationMultiCurrency> false </organizationMultiCurrency> <organizationName> Ox </organizationName> <profileId> sdfgsdfg </profileId> <roleId> sdfgsdfg </roleId> <userDefaultCurrencyIsoCode xsi:nil="true"/> <userEmail> ###@gmail.com </userEmail> <userFullName> ### ### </userFullName> <userId> asdfasdf </userId> <userLanguage> en_US </userLanguage> <userLocale> en_US </userLocale> <userName> [email protected] </userName> <userTimeZone> America/Chicago </userTimeZone> <userType> Standard </userType> <userUiSkin> Theme3 </userUiSkin> </userInfo> </result> </loginResponse> </soapenv:Body> </soapenv:Envelope> 

Anyway, I expected to feed that stuff (we'll call it data) into

$results = simplexml_load_string($data); var_dump($results); 

And that would give me all the data back... and then to access specific parts, it would be $results->body->loginResponse->blah->blah...

But It's not giving me that, it's not really giving me anything back, just an empty simple xml object...

So one website made me think I might need an XSLT to read this correctly.
Or something else made me think it's because I don't have at the top.

Help!

1
  • Maybe there's an error at reading the content of the file.... have you E_WARNING's enabled? Maybe you can see what's going on that way. Commented May 5, 2010 at 21:23

8 Answers 8

2

You can use SimpleXML but it's not quite as simple as you hope due to the use of namespaces (e.g. soapenv). Look into using SimpleXMLElement::children like:

$sxe = new SimpleXMLElement($data); $login_response = $sxe->children('soapenv', TRUE)->Body->children('', TRUE)->loginResponse->result; // Now that we have the <loginResponse> lets take a look at an item within it echo $login_response->userInfo->userEmail; 

Finally, and importantly, have you had a look at salesforce's tools & examples?

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

Comments

2

SimpleXML needs a special treatment for namespaced XML (ref.)

Comments

1

Mate,

Name spaces usually require you to make a call using children to return the namespaced elements. I would recommend using a soap client like php soapclient, but since I've never used it before there is one other possible option.

$results = simplexml_load_string($data); $xml = $results->children('http://schemas.xmlsoap.org/soap/envelope/'); var_dump($xml); 

I believe that's how it works.

Comments

0

For what it's worth, you may find you have an easier time using a PHP SoapClient for this task. O'Reilly has a good tutorial on PHP SOAP.

Comments

0

Also checkout the PHP Toolkit for making SOAP calls to Salesforce.com

Comments

0

I try to follow the syntax by salathe. But children('soapenv', TRUE) doens't work for me, Jason's children('http://schemas.xmlsoap.org/soap/envelope/') work.

Therefore, to read the field value CreatedDate in Salesforce Outbound Message, I need following code:

$rcXML->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('http://soap.sforce.com/2005/09/outbound')->notifications->Notification->sObject->children('urn:sobject.enterprise.soap.sforce.com')->CreatedDate 

To help you understand how it work, I write a post with sames code and xml which shall be easier to understand.

http://amigotechnotes.wordpress.com/2013/11/16/parse-xml-with-namespace-by-simplexml-in-php/

Comments

0

Parsing soap responses with SimpleXML has a brilliant and concise example of multi-namespace XML parsing.

For anyone wanting to get at the RateResponse from the UPS Rating API, here's how :

// $client is your SoapClient object $dom = new DOMDocument; $dom->loadXML($client->__getLastResponse()); $xml = simplexml_load_string($dom->saveXML(), NULL, NULL, 'http://schemas.xmlsoap.org/soap/envelope/'); $RateResponse = $xml->xpath('/soapenv:Envelope/soapenv:Body')[0]->children('rate', true)->RateResponse; foreach($RateResponse->RatedShipment as $RatedShipment) { print_r((array)$RatedShipment); } 

Comments

0

For SAOP request, we can parse easily with a short code by replacing the SOAP-ENV: tag with blank

$response = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body>'; $response = html_entity_decode($response); $response = str_replace(['soapenv:', 'ns1:', ':ns1', 'SOAP-ENV:'], ['', '', '', ''], $response); $objXmlData = simplexml_load_string($response); 

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.