简体   繁体   中英

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?

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

这应该对您有用:)

$xml['status'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM