I have searched extensively for this answer. Maybe it's just too simple to be posted. Please forgive me I am missing something really obvious. I am trying to use a piece of data (number value) from a GET request (I think it is the r_object value) that I access via a URL with an API key.

I am using an external service (called "teleduino") to read data (moisture/voltage readings) from a microprocessor unit (Arduino Uno with Ethernet shield). I use my personal API key from teleduino and the teleduino service uses php GET requests (in a JSON format I think). I can load the results into an iframe on my web page using javascript, but I need to use a real variable of the data not just view it in an iframe.

For example, if I load "http://us01.proxy.teleduino.org/api/1.0/328.php?k={my-key-here}&r=getAnalogInput&pin=16" into an iframe I get something like this (when the device is online) :

{status":200,"message":"OK","response":{"result:1,"time":0.22702789306641,"values":[877]}}

The number in the square brackets is the value I need to extract as a simple variable so I can display it on the page (instead of using iframes) and also use it in mathematical calculations and other functions.

That value I need (in the square brackets after "values") is I think the "r _object" in the $GET request because when I observe the URL : r=getAnalogInput&pin=16 is the crucial data I need. (It is the number of a voltage given on pin16 of that microprocessor, which refers to the soil moisture of that plant).

I have searched extensively but cannot find out how to load that value from the get request into a variable so I can use it in the javascript on my page.

I assume it is some simple php like

moisture = $_GET[URL,"values"] ....etc etc

I am assuming it is the php r_object. And I assume I need to use php or ajax as the data is coming from an outside server (the teleduino service), so pure javascript will not work.

I am happy to write some php onto the php file of my webpage, but I do not know what that php should be.

ANY ideas would be greatly appreciated - thank you so much!!!

有帮助吗?

解决方案

$_GET is for getting parameters that were passed as parameters to your PHP script, not results from a remote API. I think this is what you want:

$results = file_get_contents($url);
$data = json_decode($results, true);
$moisture = $data['response']['values'][0];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top