Author Topic: PHP cURL request to Ambients REST API not pullng data.  (Read 1312 times)

0 Members and 1 Guest are viewing this topic.

Offline Bigfootbuilt

  • Member
  • *
  • Posts: 22
PHP cURL request to Ambients REST API not pullng data.
« on: February 17, 2025, 09:34:01 AM »
Hello, I'm trying to display current conditions in text format by using PHP/cURL to pull data from Ambient's REST API to no avail. I'm not getting any error messages either, just empty fields after Temperature and Humidity.  I've replaced "my app key" and "my api key" with my real keys, and the device ID. I think the problem might be the device ID. I am using the end of the URL on my dashboard page in ambient as my device ID. Is that right? If not, where do I find the device ID? Or is something else wrong with the code?

   <?php



// Replace with your actual API keys

$applicationKey = "MY APP KEY";

$deviceKey = "MY DEVICE KEY";



// Build the API URL

$apiUrl = "https://rt.ambientweather.net/data/current/MY DEVICE ID/?applicationkey=" . $applicationKey . "&apiKey=" . $deviceKey . "&field=tempf,humidity,barom,winddir,windspeedmph";



// Initialize cURL

$curl = curl_init();



// Set cURL options

curl_setopt($curl, CURLOPT_URL, $apiUrl);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);



// Execute the request

$response = curl_exec($curl);



// Check for errors

if (curl_errno($curl)) {

    echo "Error: " . curl_error($curl);

} else {

    // Decode JSON response

    $weatherData = json_decode($response, true);



    // Access weather data

    echo "Temperature (F): " . $weatherData["tempf"] . "<br>";

    echo "Humidity: " . $weatherData["humidity"] . "<br>";

    // ... access other data based on the "field" parameter

}



// Close cURL handle

curl_close($curl);



?>

Offline hymrog

  • Senior Member
  • **
  • Posts: 64
    • Gahanna Weather
Re: PHP cURL request to Ambients REST API not pullng data.
« Reply #1 on: February 18, 2025, 05:41:30 PM »
Hi -

Try this to get your data for $apiUrl

Code: [Select]
$apiUrl = https://rt.ambientweather.net/v1/devices?apiKey=“apiKey”&applicationKey=“applicationKey”
Also you need to generate two keys an apiKey and applicationKey both are required to get your data.

g

« Last Edit: February 19, 2025, 06:40:30 AM by hymrog »

Offline Bigfootbuilt

  • Member
  • *
  • Posts: 22
Re: PHP cURL request to Ambients REST API not pullng data.
« Reply #2 on: February 19, 2025, 10:32:14 AM »
Thanks for the reply. I tried this and now the page won't load. It throws up a syntrax error. I think it has something to do with quotation marks. I tried it with and without them. Am I missing something?

Offline hymrog

  • Senior Member
  • **
  • Posts: 64
    • Gahanna Weather
Re: PHP cURL request to Ambients REST API not pullng data.
« Reply #3 on: February 19, 2025, 10:55:30 AM »
Hi -

Code: [Select]
$apiUrl = 'https://rt.ambientweather.net/v1/devices?apiKey="your apiKey"&applicationKey="your applicationKey"';

$ch = curl_init();




// Set cURL options

curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);


Use without quotes. 

Did you generate an application key and an api key?  As noted you need both.  If you get errors please post.

g


Offline Bigfootbuilt

  • Member
  • *
  • Posts: 22
Re: PHP cURL request to Ambients REST API not pullng data.
« Reply #4 on: February 19, 2025, 11:52:17 AM »
Yes, and I entered them in the link. Still nothing. Tried it with and without quotes. Entered both keys. No more syntax error, but the readings won't load. I even tried replacing v1 with v2 and v3, etc. Has anyone ever had success in pulling and displaying data in just a simple text format from Ambient with PHP/cURL?
« Last Edit: February 19, 2025, 11:57:30 AM by Bigfootbuilt »

Offline wvdkuil

  • Wim van der kuil
  • Forecaster
  • *****
  • Posts: 2068
    • My PWS at Leuven Belgium Europe
Re: PHP cURL request to Ambients REST API not pullng data.
« Reply #5 on: February 19, 2025, 12:32:01 PM »
Yes, and I entered them in the link. Still nothing. Tried it with and without quotes. Entered both keys. No more syntax error, but the readings won't load. I even tried replacing v1 with v2 and v3, etc. Has anyone ever had success in pulling and displaying data in just a simple text format from Ambient with PHP/cURL?

The application key = the key you got when entering your appication

The apikey = your apikey for every user of your application another one

In my case both keys are 64 characters.
Yes that is long and yes you have to be carefull copying those.

You should first test this in your browser to make sure that it is working. See attached screenshot.

How should it look in your browser
https://rt.ambientweather.net/v1/devices?apiKey=847ed9304bexxxxxxxb34cd2d530d14b26&applicationKey=2d94ac7e4xxxxx19b4e284

Returns following data as a jsonffile:
[{"macAddress":"B8:D8:12:60:40:A7","lastData":{"dateutc":1739985600000,"winddir":67,"windspeedmph":0,"windgustmph":0.9,"maxdailygust":4,"windgustdir":302,"winddir_avg2m":66,"windspdmph_avg2m":0,"winddir_avg10m":279,"windspdmph_avg10m":0,"tempf":42.8,"humidity":62,"baromrelin":30.11,"baromabsin":29.96,"tempinf":72.7,"humidityin":25,"hourlyrainin":0,"dailyrainin":0,"monthlyrainin":1.22,"yearlyrainin":7.22,"battin":0,"battout":0,"feelsLike":42.8,"dewPoint":30.69,"feelsLikein":70.8,"dewPointin":34.8,"lastRain":"2025-02-13T10:13:00.000Z","tz":"Europe/Luxembourg","date":"2025-02-19T17:20:00.000Z"},"info":{"name":"Sluispark","coords":{"coords":{"lon":4.700163841247559,"lat":50.88512473189906},"address":"Sluisstraat 16, 3000 Leuven, Belgium","location":"Leuven","elevation":19.31222343444824,"geo":{"type":"Point","coordinates":[4.700163841247559,50.88512473189906]}}}}]

« Last Edit: February 19, 2025, 12:33:38 PM by wvdkuil »

Offline hymrog

  • Senior Member
  • **
  • Posts: 64
    • Gahanna Weather
Re: PHP cURL request to Ambients REST API not pullng data.
« Reply #6 on: February 19, 2025, 01:08:03 PM »
Working code based on your previous example/attempt:

Code: [Select]
<?php

$apiUrl = &#39;https://rt.ambientweather.net/v1/devices?apiKey=99c66xxxxxxxxxxxxxx137d4991b7b481b60ae72ca4&applicationKey=e1f230dxxxxxxxxxxxxxxxxxb67ff3625efcade7&#39;;

$ch = curl_init();


// Set cURL options

curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);


$response = curl_exec($ch);


if (curl_errno($ch)) {

$error_msg = curl_error($ch);


} else {



$data = json_decode($response,true);

echo "Temperature (F): " . $data[0][&#39;lastData&#39;][&#39;tempf&#39;] . "<br>";

//echo "Humidity: " . $data["humidity"] . "<br>";

// ... access other data based on the "field" parameter

}


curl_close($ch);


?>



Response when the script is run the browser:

Temperature(F): 21.6


Edit…sorry about the confusion with the &#39…did not catch that ‘ ‘ marks were changed to html coded character set when I posted up the code. 
« Last Edit: February 19, 2025, 05:57:22 PM by hymrog »

Offline Bigfootbuilt

  • Member
  • *
  • Posts: 22
Re: PHP cURL request to Ambients REST API not pullng data.
« Reply #7 on: February 19, 2025, 01:26:29 PM »
Thanks for the replies. Is the "$#39"; supposed to be on both ends of the link like that? Or was that just a copy/pasting error?

Offline Bigfootbuilt

  • Member
  • *
  • Posts: 22
Re: PHP cURL request to Ambients REST API not pullng data.
« Reply #8 on: February 19, 2025, 01:45:05 PM »
Yes, and I entered them in the link. Still nothing. Tried it with and without quotes. Entered both keys. No more syntax error, but the readings won't load. I even tried replacing v1 with v2 and v3, etc. Has anyone ever had success in pulling and displaying data in just a simple text format from Ambient with PHP/cURL?

The application key = the key you got when entering your appication

The apikey = your apikey for every user of your application another one

In my case both keys are 64 characters.
Yes that is long and yes you have to be carefull copying those.

You should first test this in your browser to make sure that it is working. See attached screenshot.

How should it look in your browser
https://rt.ambientweather.net/v1/devices?apiKey=847ed9304bexxxxxxxb34cd2d530d14b26&applicationKey=2d94ac7e4xxxxx19b4e284

Returns following data as a jsonffile:
[{"macAddress":"B8:D8:12:60:40:A7","lastData":{"dateutc":1739985600000,"winddir":67,"windspeedmph":0,"windgustmph":0.9,"maxdailygust":4,"windgustdir":302,"winddir_avg2m":66,"windspdmph_avg2m":0,"winddir_avg10m":279,"windspdmph_avg10m":0,"tempf":42.8,"humidity":62,"baromrelin":30.11,"baromabsin":29.96,"tempinf":72.7,"humidityin":25,"hourlyrainin":0,"dailyrainin":0,"monthlyrainin":1.22,"yearlyrainin":7.22,"battin":0,"battout":0,"feelsLike":42.8,"dewPoint":30.69,"feelsLikein":70.8,"dewPointin":34.8,"lastRain":"2025-02-13T10:13:00.000Z","tz":"Europe/Luxembourg","date":"2025-02-19T17:20:00.000Z"},"info":{"name":"Sluispark","coords":{"coords":{"lon":4.700163841247559,"lat":50.88512473189906},"address":"Sluisstraat 16, 3000 Leuven, Belgium","location":"Leuven","elevation":19.31222343444824,"geo":{"type":"Point","coordinates":[4.700163841247559,50.88512473189906]}}}}]

Ok, when I enter that URL in the browser, that works. It just won't return anything when entered into code. I tried the code that Hymrog posted here but that isn't working either. The "&#39;" entries throughout the code are confusing me. What is that supposed to be?

Offline Paulcheffus

  • Member
  • *
  • Posts: 45
Re: PHP cURL request to Ambients REST API not pullng data.
« Reply #9 on: February 19, 2025, 01:52:21 PM »
Hi

&#39; is the code for an apostrophe.

Not sure why it’s being used though as all my curl commands just have the ‘ in them directly.

Cheers

Paul
« Last Edit: February 19, 2025, 01:54:06 PM by Paulcheffus »
Ecowitt - WS2910 & GW3000
Ecowitt Sensors - WS69, WH32P, WH57 & WH46D

Offline wvdkuil

  • Wim van der kuil
  • Forecaster
  • *****
  • Posts: 2068
    • My PWS at Leuven Belgium Europe
Re: PHP cURL request to Ambients REST API not pullng data.
« Reply #10 on: February 19, 2025, 02:28:43 PM »
Ok, when I enter that URL in the browser, that works. It just won't return anything when entered into code. I tried the code that Hymrog posted here but that isn't working either. The "&#39;" entries throughout the code are confusing me. What is that supposed to be?

What is returned is a json text file.
That one has to be changed to a format your scripts can process.

F.i. when using PHP as the language it is
$jsonobj = reasult_of_your_curl;

$arr = json_decode($jsonobj, true);
echo '<pre>'.print_r($arr,true);


read: https://www.w3schools.com/php/func_json_decode.asp

Succes,
Wim


Offline Bigfootbuilt

  • Member
  • *
  • Posts: 22
Re: PHP cURL request to Ambients REST API not pullng data.
« Reply #11 on: February 20, 2025, 12:14:02 AM »
Working code based on your previous example/attempt:

Code: [Select]
<?php

$apiUrl = &#39;https://rt.ambientweather.net/v1/devices?apiKey=99c66xxxxxxxxxxxxxx137d4991b7b481b60ae72ca4&applicationKey=e1f230dxxxxxxxxxxxxxxxxxb67ff3625efcade7&#39;;

$ch = curl_init();


// Set cURL options

curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);


$response = curl_exec($ch);


if (curl_errno($ch)) {

$error_msg = curl_error($ch);


} else {



$data = json_decode($response,true);

echo "Temperature (F): " . $data[0][&#39;lastData&#39;][&#39;tempf&#39;] . "<br>";

//echo "Humidity: " . $data["humidity"] . "<br>";

// ... access other data based on the "field" parameter

}


curl_close($ch);


?>



Response when the script is run the browser:

Temperature(F): 21.6


Edit…sorry about the confusion with the &#39…did not catch that ‘ ‘ marks were changed to html coded character set when I posted up the code.

Ok, I tried your code using my api and application keys and still can't get any output other than "temperature:" but no readings. I'm not getting any syntax or PHP errors. Can anyone see anything wrong (other than the &#39 this board keeps replacing my " with)?

Code: [Select]
    <?php

$apiUrl = &#39;https://rt.ambientweather.net/v1/devices?apiKey=MY16 DIGIT API KEY&applicationKey=MY 16 DIGIT APP KEY&#39;;

$ch = curl_init();


// Set cURL options

curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);


$response = curl_exec($ch);


if (curl_errno($ch)) {

$error_msg = curl_error($ch);


} else {



$data = json_decode($response,true);

echo "Temperature (F): " . $data[0][&#39;lastData&#39;][&#39;;tempf&#39;] . "<br>";

//echo "Humidity: " . $data["humidity"] . "<br>";

// ... access other data based on the "field" parameter

}


curl_close($ch);


?>

Offline Bigfootbuilt

  • Member
  • *
  • Posts: 22
Re: PHP cURL request to Ambients REST API not pullng data.
« Reply #12 on: February 20, 2025, 12:17:24 AM »
Does anyone see anything wrong with this? Still can't get any readings. Not getting any errors from VS, just no output. What am I missing here?
    <?php   
   
   $apiUrl = 'https://rt.ambientweather.net/v1/devices?apiKey=MY API KEY&applicationKey=MY APPLICATION KEY";
   
   $ch = curl_init();
   
   
   // Set cURL options
   
   curl_setopt($ch, CURLOPT_URL, $apiUrl);   
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($ch, CURLOPT_FAILONERROR, true);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      
      
   $response = curl_exec($ch);
   

   if (curl_errno($ch)) {
      
      $error_msg = curl_error($ch);
      
      
   } else {
      
      
      
      $data = json_decode($response,true);
      
      echo "Temperature (F): " . $data[0]['lastData'][';tempf'] . "<br>";
      
      //echo "Humidity: " . $data["humidity"] . "<br>";
      
      // ... access other data based on the "field" parameter
      
   }
   
      
   curl_close($ch);
   
      
?>

Offline Paulcheffus

  • Member
  • *
  • Posts: 45
Re: PHP cURL request to Ambients REST API not pullng data.
« Reply #13 on: February 20, 2025, 02:44:48 AM »
Hi

That code will only show temperature as the humidity line is commented out.

Why have you got two lines setting the CURLOPT_RETURNTRANSFER option? You only need one.

If it does fail you are not reporting the error from curl. You need to add
echo $error_msg;
after the error has been assigned to the variable.

One other thing you could add is
print_r($data);
to show what is actually being returned from the API.

Cheers

Paul
« Last Edit: February 20, 2025, 02:49:52 AM by Paulcheffus »
Ecowitt - WS2910 & GW3000
Ecowitt Sensors - WS69, WH32P, WH57 & WH46D

Offline Bigfootbuilt

  • Member
  • *
  • Posts: 22
Re: PHP cURL request to Ambients REST API not pullng data.
« Reply #14 on: February 20, 2025, 08:24:32 AM »
Hi

That code will only show temperature as the humidity line is commented out.

Why have you got two lines setting the CURLOPT_RETURNTRANSFER option? You only need one.

If it does fail you are not reporting the error from curl. You need to add
echo $error_msg;
after the error has been assigned to the variable.

One other thing you could add is
print_r($data);
to show what is actually being returned from the API.

Cheers

Paul

Thanks for the suggestion. If I can only get temperature, I would be thrilled to get anything at this point. I removed the duplicate line and added the "print_r($data); but that just prints the entire unfiltereed result, like this (edited for security)...

Array (
  • => Array ( [macAddress] => xx:xx:xx:xx [lastData] => Array ( [dateutc] => 1740057300000 [tempf] => 32.5 [humidity] => 77 [windspeedmph] => 5.37 [windgustmph] => 10.29 [maxdailygust] => 14.76 [winddir] => 293 [winddir_avg10m] => 293 [uv] => 0 [solarradiation] => 36.14 [hourlyrainin] => 0 [eventrainin] => 0 [dailyrainin] => 0 [weeklyrainin] => 0.437 [monthlyrainin] => 5.138 [yearlyrainin] => 7.567 [battout] => 1 [tempinf] => 36.5 [humidityin] => 74 [baromrelin] => 30.141 [baromabsin] => 29.282 [battin] => 1 [soilhum1] => 59 [battsm1] => 1 [lightning_day] => 0 [lightning_time] => 1739705501000 [lightning_distance] => 14.91 [batt_lightning] => 0 [feelsLike] => 27.34 [dewPoint] => 26.11 [feelsLikein] => 36.5 [dewPointin] => 29 [lastRain] => 2025-02-19T17:39:00.000Z [lightning_hour] => 0 [tz] => America/New_York [date] => 2025-02-20T13:15:00.000Z ) [info] => Array ( [name] => Centerville Wx [coords] => Array ( [coords] => Array ( [lat] => 34 [lon] => -82 ) [address] => xxxxxxxxxx, USA [location] => xxxxx [elevation] => 249.53344726562 [address_components] => Array (
  • => Array ( [long_name] => 2103 [short_name] => 2103 [types] => Array (
  • => street_number ) ) [1] => Array ( [long_name] => xxxxx [short_name] => xxxxx [types] => Array (
  • => route ) ) [2] => Array ( [long_name] => xxxxxx [short_name] => xxxxx [types] => Array (
  • => locality [1] => political ) ) [3] => Array ( [long_name] => xxxxxx [short_name] => xxxxxx [types] => Array (
  • => administrative_area_level_2 [1] => political ) ) [4] => Array ( [long_name] => South Carolina [short_name] => SC [types] => Array (
  • => administrative_area_level_1 [1] => political ) ) [5] => Array ( [long_name] => United States [short_name] => US [types] => Array (
  • => country [1] => political ) ) [6] => Array ( [long_name] => xxxxx [short_name] => xxxxx [types] => Array (
  • => postal_code ) ) [7] => Array ( [long_name] => 5133 [short_name] => 5133 [types] => Array (
  • => postal_code_suffix ) ) ) [geo] => Array ( [type] => Point [coordinates] => Array (
  • => -xxxxxxxx [1] => xxxxxx ) ) ) ) ) ) Temperature (F):

Offline hymrog

  • Senior Member
  • **
  • Posts: 64
    • Gahanna Weather
Re: PHP cURL request to Ambients REST API not pullng data.
« Reply #15 on: February 20, 2025, 09:00:19 AM »
Quote
Why have you got two lines setting the CURLOPT_RETURNTRANSFER option? You only need one
  My bad on that one.  Using CodeRunner to test the script and it must have auto completed that particular line.  My original intention was to have    CURLOPT_HEADER, false...again this just something to get the OP started.

Regarding the humidity field...Just too lazy to uncomment but it works as intended.

OP - I see you have been able to get the info you are looking for.  To get the info you will need to "see" the temp and humidity you can do the following:

Code: [Select]
echo "Temperature(F): " . $data[0]['lastData']['tempf'] . "<br>";
echo "Humidity:  " . $data[0]['lastData']['humidity'] . "<br>";


g

Offline Bigfootbuilt

  • Member
  • *
  • Posts: 22
Re: PHP cURL request to Ambients REST API not pullng data.
« Reply #16 on: February 20, 2025, 09:12:45 AM »
Quote
Why have you got two lines setting the CURLOPT_RETURNTRANSFER option? You only need one
  My bad on that one.  Using CodeRunner to test the script and it must have auto completed that particular line.  My original intention was to have    CURLOPT_HEADER, false...again this just something to get the OP started.

Regarding the humidity field...Just too lazy to uncomment but it works as intended.

OP - I see you have been able to get the info you are looking for.  To get the info you will need to "see" the temp and humidity you can do the following:

Code: [Select]
echo "Temperature(F): " . $data[0]['lastData']['tempf'] . "<br>";
echo "Humidity:  " . $data[0]['lastData']['humidity'] . "<br>";


g

Thanks for yalls time and effort guys. The code just isn't working on my end. It either returns all unfiltered/unparsed results or nothing. I've tried it on 2 different webservers with PHP and cURL enabled. I'm out of hair to pull out lol.

Thanks again


Offline Paulcheffus

  • Member
  • *
  • Posts: 45
Re: PHP cURL request to Ambients REST API not pullng data.
« Reply #17 on: February 20, 2025, 09:54:02 AM »
Hi

If you are getting the unfiltered results then it is returning data.

Can you share your updated code (without the personal bits).

Cheers

Paul
Ecowitt - WS2910 & GW3000
Ecowitt Sensors - WS69, WH32P, WH57 & WH46D

Offline Bigfootbuilt

  • Member
  • *
  • Posts: 22
Re: PHP cURL request to Ambients REST API not pullng data.
« Reply #18 on: February 20, 2025, 11:49:58 PM »
Got it! I started over and used the tips you all posted here and put it on a separate PHP page, then used the <?php include 'api.php' ?> tag to display the output. Dunno why it wouldn't work all on the same page, but this is actually works out better because it is easier to format it into columns, etc. Thanks all for the help! Maybe that trick will help someone else if you run into problems.

 

anything