How to deserialise thingspeak feeds xml data with PHP | ThingSpeak API | Forum

Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed sp_TopicIcon
How to deserialise thingspeak feeds xml data with PHP
No permission to create posts
December 21, 2016
1:17 am
Avatar
mchampio
New Member
Members
Forum Posts: 2
Member Since:
December 21, 2016
sp_UserOfflineSmall Offline

I have data in thing speak that I can get with

$url = "https://api.thingspeak.com/channels/203731/feeds.xml?results=500";
$xml = simplexml_load_file($url);
$channel_name = (
string) $xml->name;
print_r($channel_name);

This is one of my attempts to display the feeds fields

foreach ($xml->feeds as $feed) {
/      print_r($feed);
}

The channel name displays when I run this code, but I have tried every combination I can think of to get the feed data, but either get nothing displayed or an error message

What code do I use to get the feeds data (field1, Field2 etc)

Alternatively, is there documentation specific to the feeds data or other software you can point me to

December 21, 2016
8:01 am
Avatar
akhila

MathWorks
Members
Forum Posts: 21
Member Since:
August 10, 2015
sp_UserOfflineSmall Offline

Hi, 

Here is the doc regarding retrieving of channel feeds: 

https://www.mathworks.com/help/thingspeak/get-a-channel-feed.html

 

Thanks

Akhila 

December 21, 2016
1:40 pm
Avatar
jvasudev

MathWorks
Members
Forum Posts: 13
Member Since:
April 29, 2016
sp_UserOfflineSmall Offline

If you are looking to just fetch the data from the individual fields from the overall feeds data, you can use something like this,

<?php
$url = "https://api.thingspeak.com/channels/203731/feeds.xml?results=500";
$xml = simplexml_load_file($url);
$channel_name = (string) $xml->name;
print_r($channel_name);

$field1 = $xml->xpath('//feed/field1'); //gets all <field1/> whose parent is <feed/>
print_r($field1);
$field2 = $xml->xpath('//feed/field2'); //gets all <field2/> whose parent is <feed/>
print_r($field2);
?>

December 21, 2016
2:47 pm
Avatar
mchampio
New Member
Members
Forum Posts: 2
Member Since:
December 21, 2016
sp_UserOfflineSmall Offline


Thank you so much jvasudev, that is exactly what I was looking for.Smile

I added the loop at the bottom, so noobs like me could see how to use the data in the field

<?php
$url = "https://api.thingspeak.com/channels/203731/feeds.xml?results=500";
$xml = simplexml_load_file($url);
$description = (string) $xml->description;
print_r($description);
$field1 = $xml->xpath('//feed/field1'); // get all <field1/> whose parent is <feed/>
print_r($field1);
$field2 = $xml->xpath('//feed/field2'); //gets all <field2/> whose parent is <feed/>
print_r($field2);
foreach ($field2 as $onefield2) {
  echo $onefield2;
  echo '<br/>';
}
?>

July 25, 2019
2:35 am
Avatar
anitahk
New Member
Members
Forum Posts: 4
Member Since:
July 25, 2019
sp_UserOfflineSmall Offline

I need help to read the data from thingspeak channel into my PHP program. I have tried this code for my channel number 819747. taken this code from above post

php code to extract thingspeak

description;
print_r($description);
$field1 = $xml->xpath('//feed/field1'); // get all whose parent is
print_r($field1);
$field2 = $xml->xpath('//feed/field2'); //gets all whose parent is
print_r($field2);
foreach ($field2 as $onefield2) {
echo $onefield2;
echo '
';
}
?>

but i am getting following warnings and errors when I run the php code in my browser

( ! ) Warning: simplexml_load_file(https://api.thingspeak.com/channels/819747/feeds.xml?results=500): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in C:\wamp\www\thingspk2.php on line 9 --> $xml = simplexml_load_file($url);
Call Stack
# Time Memory Function Location
1 0.0020 242432 {main}( ) ..\thingspk2.php:0
2 0.0020 242704 simplexml_load_file ( ) ..\thingspk2.php:9

( ! ) Warning: simplexml_load_file(): I/O warning : failed to load external entity "https://api.thingspeak.com/channels/819747/feeds.xml?results=500&quot; in C:\wamp\www\thingspk2.php on line 9 --> $xml = simplexml_load_file($url);
Call Stack
# Time Memory Function Location
1 0.0020 242432 {main}( ) ..\thingspk2.php:0
2 0.0020 242704 simplexml_load_file ( ) ..\thingspk2.php:9

( ! ) Notice: Trying to get property of non-object in C:\wamp\www\thingspk2.php on line 10 --> $description = (string) $xml->description;
Call Stack
# Time Memory Function Location
1 0.0020 242432 {main}( ) ..\thingspk2.php:0

( ! ) Fatal error: Call to a member function xpath() on a non-object in C:\wamp\www\thingspk2.php on line 12 = $field1 = $xml->xpath('//feed/field1');
Call Stack
# Time Memory Function Location
1 0.0020 242432 {main}( ) ..\thingspk2.php:0

July 25, 2019
4:00 am
Avatar
anitahk
New Member
Members
Forum Posts: 4
Member Since:
July 25, 2019
sp_UserOfflineSmall Offline

Sorry this is the complete program - i have posted incomplete code but the problem is same. please help. i am working on this

php code to extract thingspeak

description;
print_r($description);
$field1 = $xml->xpath('//feed/field1'); // get all whose parent is
print_r($field1);
$field2 = $xml->xpath('//feed/field2'); //gets all whose parent is
print_r($field2);
foreach ($field2 as $onefield2) {
echo $onefield2;
echo '
';
}
?>

Forum Timezone: America/New_York

Most Users Ever Online: 166

Currently Online:
17 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 272

Vinod: 240

piajola: 95

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 8665

Moderators: 1

Admins: 2

Forum Stats:

Groups: 3

Forums: 14

Topics: 1600

Posts: 5760

Newest Members:

Team_mafia, icXu, emorphistechno, VTVMART, manimozhi, adamhebrew

Moderators: cstapels: 460

Administrators: Hans: 405, lee: 457