I need help looping through a complex XML response I receive from making an api call using PHP cURL. I think I've tried everything I could find here on SO and in the PHP docs. Nothing works. I need help understanding how to do this. For instance if I wanted to print the admin address, how would I do this?
PHP cURL
$ch = curl_init($connection_details['api_host_port']); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $data); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); $response = curl_exec($ch); // // // //parse xml string into SimpleXML objects $xml = new SimpleXMLElement($response); print_r($xml); Full Response
SimpleXMLElement Object ( [header] => SimpleXMLElement Object ( [version] => 0.9 ) [body] => SimpleXMLElement Object ( [data_block] => SimpleXMLElement Object ( [dt_assoc] => SimpleXMLElement Object ( [item] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [key] => attributes ) [dt_assoc] => SimpleXMLElement Object ( [item] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [key] => affiliate_id ) ) [1] => 1 [2] => 2020-08-24 21:55:12 [3] => SimpleXMLElement Object ( [@attributes] => Array ( [key] => nameserver_list ) [dt_array] => SimpleXMLElement Object ( [item] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [key] => 0 ) [dt_assoc] => SimpleXMLElement Object ( [item] => Array ( [0] => ns1.systemdns.com [1] => SimpleXMLElement Object ( [@attributes] => Array ( [key] => ipaddress ) ) [2] => 1 ) ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [key] => 1 ) [dt_assoc] => SimpleXMLElement Object ( [item] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [key] => ipaddress ) ) [1] => 2 [2] => ns2.systemdns.com ) ) ) ) ) ) [4] => SimpleXMLElement Object ( [@attributes] => Array ( [key] => tld_data ) ) [5] => 2021-08-24 21:55:10 [6] => NONE [7] => 2021-08-24 21:55:10 [8] => SimpleXMLElement Object ( [@attributes] => Array ( [key] => contact_set ) [dt_assoc] => SimpleXMLElement Object ( [item] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [key] => owner ) [dt_assoc] => SimpleXMLElement Object ( [item] => Array ( [0] => +1.5556667777 [1] => SimpleXMLElement Object ( [@attributes] => Array ( [key] => address2 ) ) [2] => Miami [3] => John [4] => NA [5] => FL [6] => SimpleXMLElement Object ( [@attributes] => Array ( [key] => address3 ) ) [7] => active [8] => SimpleXMLElement Object ( [@attributes] => Array ( [key] => fax ) ) [9] => 3200 Northwest 67th Avenue [10] => Doe [11] => [email protected] [12] => US [13] => 33122 ) ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [key] => admin ) [dt_assoc] => SimpleXMLElement Object ( [item] => Array ( [0] => Miami [1] => John [2] => FL [3] => NA [4] => SimpleXMLElement Object ( [@attributes] => Array ( [key] => address3 ) ) [5] => active [6] => 3200 Northwest 67th Avenue [7] => Doe [8] => SimpleXMLElement Object ( [@attributes] => Array ( [key] => fax ) ) [9] => [email protected] [10] => US [11] => 33122 [12] => +1.5556667777 [13] => SimpleXMLElement Object ( [@attributes] => Array ( [key] => address2 ) ) ) ) ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [key] => billing ) [dt_assoc] => SimpleXMLElement Object ( [item] => Array ( [0] => +1.5556667777 [1] => SimpleXMLElement Object ( [@attributes] => Array ( [key] => address2 ) ) [2] => FL [3] => NA [4] => Miami [5] => John [6] => [email protected] [7] => 33122 [8] => US [9] => SimpleXMLElement Object ( [@attributes] => Array ( [key] => address3 ) ) [10] => 3200 Northwest 67th Avenue [11] => active [12] => SimpleXMLElement Object ( [@attributes] => Array ( [key] => fax ) ) [13] => Doe ) ) ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [key] => tech ) [dt_assoc] => SimpleXMLElement Object ( [item] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [key] => address2 ) ) [1] => +1.5556667777 [2] => Jane Doe [3] => Los Angeles [4] => CA [5] => Company Name [6] => SimpleXMLElement Object ( [@attributes] => Array ( [key] => fax ) ) [7] => active [8] => 555 My Street [9] => Jane Doe [10] => SimpleXMLElement Object ( [@attributes] => Array ( [key] => address3 ) ) [11] => US [12] => 77788 [13] => [email protected] ) ) ) ) ) ) [9] => 1 [10] => 2020-08-24 21:55:10 [11] => 0 ) ) ) [1] => DOMAIN [2] => 200 [3] => XCP [4] => REPLY [5] => Query Successful [6] => 1 ) ) ) ) ) per request EDIT WITH $xml->asXML():
0.9 Query Successful 200 1 XCP DOMAIN REPLY 1 John NA US +1.5556667777 Doe 3200 Northwest 67th Avenue Miami active [email protected] 33122 FL active Los Angeles [email protected] 77788 CA Company Name Jane Doe +1.5556667777 US 555 My Street Jane Doe +1.5556667777 US NA John Doe 3200 Northwest 67th Avenue [email protected] Miami active 33122 FL 33122 active Miami [email protected] FL John NA US +1.5556667777 Doe 3200 Northwest 67th Avenue ns1.systemdns.com 1 2 ns2.systemdns.com 2020-08-24 21:55:12 2021-08-24 21:55:10 NONE 2020-08-24 21:55:10 0 1 2021-08-24 21:55:10
echo $xml->asXML();looks like?