-1

I am using this code to extract field data from a thingspeak xml stream

$url = "https://api.thingspeak.com/channels/203731/feeds.xml?results=500";
$xml = simplexml_load_file($url);
$channel_name = $xml->name; 
print_r($channel_name);
foreach ($xml->feeds as $feed) {
    print_r($field1);
}

The channel_name displays but not the field1. I can't find answers on this site that specifically show extracting data from a type="array" within an xml file Here is the edited version of the xml returned from thingspeak that shows the feeds records " " Pothole Locations " " " 1.00 " 2.00 " " " 1.00 " 2.00 " " "

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • What did you tried? There so many ways to get it out. – Naga Dec 21 '16 at 01:14
  • Here is the code I tried using aconnelly's suggestion. It displays the channel_name but no field1 data – Mark Champion Dec 21 '16 at 02:14
  • $url = "https://api.thingspeak.com/channels/203731/feeds.xml?results=500"; $xml = simplexml_load_file($url); //print_r($xml); //$xml = new SimpleXMLElement($url); $channel_name = (string) $xml->name; print_r($channel_name); foreach ($xml->feeds as $feed) { print_r($field1); } ?> – Mark Champion Dec 21 '16 at 02:15

1 Answers1

0

You could try

$xml = new SimpleXMLElement($xmlString);

foreach ($xml->feeds as $feed) {
    ....
}

or for an actual array

$xml = simplexml_load_string($xmlstring, "SimpleXMLElement", LIBXML_NOCDATA);
$array = json_decode(json_encode($xml), true);
aconnelly
  • 662
  • 1
  • 6
  • 13