0

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 
8
  • 1
    Some indentation would be helpful in seeing the data structure Commented Aug 26, 2020 at 23:41
  • Could you also show what echo $xml->asXML(); looks like? Commented Aug 27, 2020 at 0:17
  • @GetSet : I dont know how to do that here. Commented Aug 27, 2020 at 1:01
  • @JackFleeting: it returns a well formatted XML string without the extra stuff. Commented Aug 27, 2020 at 14:53
  • What do you mean by "the extra stuff"? Commented Aug 27, 2020 at 22:09

1 Answer 1

1

Using the very last example in the link you provide in your comment, something like the below - using xpath - should get you the admin info:

$admin = $xml->xpath('//item[@key="admin"]//item'); foreach($admin as $item) { echo $item; } 

Output:

US Admin Example Inc. +1.4165550123x1812 CA Suite 100 active Adams [email protected] Santa Clara 90210 +1.4165550125 32 Oak Street Adler 

Or to get server info for the server w/ id 2:

$server = $xml->xpath('//item[@key="nameserver_list"]//item[@key="2"]//dt_assoc//item'); foreach($server as $item) { echo $item; } 

Output:

24.22.23.28 3 patrick.example.com 

etc.

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

2 Comments

Dude! You literally saved my life!
If I want to extract one value at a time without iteration, what would be the approach? This structure that you're presenting in your answer is new to me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.