DMSJax
DMSJax

Reputation: 1717

extract data from XML response w/PHP

When I get an API response containing this:

<?xml version="1.0" encoding="utf-8"?>
<response xmlns="http://www.XXXXXXX.com/api/" status="ok">
  <client_id>17992</client_id>
</response>

I can get the results of the <client_id> using this.

$xml = simplexml_load_string($server_output);
$client = (string) $xml->client_id;
echo $client; // produces 17992 in this case

but if I add this below, I do not get a value assigned to $response.

$response = (string) $xml->response; // produces empty value

How do I write the PHP code to check if XML response "status" = OK?

Upvotes: 7

Views: 23523

Answers (2)

michi
michi

Reputation: 6625

see this on simple applications of simplexml:
http://www.php.net/manual/en/simplexml.examples-basic.php

To access attributes of a node, do:

$xml = simplexml_load_string($x); // assume XML in $x
echo $xml['status'];

or loop through all attributes:

foreach ($xml->attributes() as $name => $value)
    echo "$name: $value <br />";

see it in action: https://eval.in/40185

Upvotes: 3

skrilled
skrilled

Reputation: 5371

This should work great for you :)

$xml['status'];

Upvotes: 0

Related Questions