I am using a cloud based contact management system API with PHP library that returns the following code when I use print_r($xml) (in View Source) when searching for a contact:
SimpleXMLElement Object
(
[id12345] => SimpleXMLElement Object
(
[id] => 12345
[name] => Bob Smith
[parentid] => 51214064
[parentcn] => EGCTeam
[flagged] => SimpleXMLElement Object
(
)
[created] => 2012-08-24T18:39:22+00:00
[viewed] => 2012-08-28T10:39:49+00:00
[updated] => 2012-08-28T10:41:05+00:00
)
[count] => 1
[status] => success
)
Normally I would use this code to access the “name” field:
$contactName = $xml->id12345->name
But since the first node includes the ID that I don’t know, how do I access the values of the contact? I tried this:
$contactName = $xml->children()->name
but it didn’t work (came out blank). Any help would be appreciated.
You can use
SimpleXMLElement::getNamefunction to get the name of the SimpleXML element and parse the id from the name by getting the string after id.For example (if $response is the SimpleXML element):