Jump to content

using php read from weather api and insert into mysql database


AJM2
Go to solution Solved by AJM2,

Recommended Posts

I currently have a weather station and website that is shareware software. I get the weather data from Wunderground and inserts into the mysql database to power the site. I would like to use the weather station api and insert directly into the database, essentially by-passing Wunderground.

I am able to read the api and see the array but unable to loop over the array and get the data I would like. The data I really want is in the lastData key/array

This is the return from the api:

array(1) { [0]=> array(3) { ["macAddress"]=> string(17) "FA:F5:C2:98:6F:29" ["lastData"]=> array(27) { ["dateutc"]=> int(1642622880000) ["tempinf"]=> float(77.9) ["humidityin"]=> int(38) ["baromrelin"]=> float(29.956) ["baromabsin"]=> float(29.132) ["tempf"]=> float(77.2) ["battout"]=> int(1) ["humidity"]=> int(47) ["winddir"]=> int(247) ["windspeedmph"]=> float(5.6) ["windgustmph"]=> float(8.1) ["maxdailygust"]=> float(15.9) ["hourlyrainin"]=> int(0) ["eventrainin"]=> int(0) ["dailyrainin"]=> int(0) ["weeklyrainin"]=> int(0) ["monthlyrainin"]=> float(0.52) ["totalrainin"]=> float(48.862) ["solarradiation"]=> float(196.47) ["uv"]=> int(1) ["feelsLike"]=> float(76.83) ["dewPoint"]=> float(55.39) ["feelsLikein"]=> float(77.2) ["dewPointin"]=> float(50.2) ["lastRain"]=> string(24) "2022-01-12T02:50:00.000Z" ["tz"]=> string(15) "America/Chicago" ["date"]=> string(24) "2022-01-19T20:08:00.000Z" } ["info"]=> array(2) { ["name"]=> string(18) "My Weather Station" ["coords"]=> array(5) { ["coords"]=> array(2) { ["lon"]=> float(-197.65635809999999) ["lat"]=> float(38.6587316) } ["address"]=> string(44) "100 Main Street, Anytown, FL 08226, USA" ["location"]=> string(10) "Anytown" ["elevation"]=> float(214.7066497802734) ["geo"]=> array(2) { ["type"]=> string(5) "Point" ["coordinates"]=> array(2) { [0]=> float(-197.65635809999999) [1]=> float(38.6587316) } } } } } }

This is the code I have:

<?php // Read JSON file 
$readjson = file_get_contents("https://api.") ; 
//Decode JSON 
$data = json_decode($readjson, true); 
//echo '<pre>' ; 
var_dump($data); ?> 
<h1>Weather Forecast for</h1>
<table> 
  <tr><th>Date</th><th>Max Temp</th><th>Min Temp</th><th>Precip</th><th>Wspd</th><th>Wgust</th><th>Cloud cover</th></tr> 
<?php foreach ($data as $key => $key_value) { 
  echo "key is: " .$key. ", "."value is: " .$key_value; echo "<br>"; ?> <tr><td><?php echo $key_value; ?></td></tr>
 

What I get for output:

2022-01-20_11-11.png

Link to comment
Share on other sites

$data is an array with one element that is an array.  That array has a lot of elements including the one you say you want.  So try:

<?php 

foreach ($data[0]['lastData'] as $key => $key_value) { 
   echo "key: $key, value: $key_value <br>";
}

Once you have figured out which keys you actually want, you should be able to access them more directly.  So for example:

 

echo "<p>Temperature (F): {$data[0]['lastData']['tempinf']}</p>";
echo "<p>Wind (mph): {$data[0]['lastData']['windspeedmph']}</p>";

 

I don't see a min/max temp value in the data you provided. 

Aside from 'lastData', there are several other keys and nested arrays in some cases.  For figuring this out in the future, aside from var_dump, print_r is often a useful development/debugging function that can be used as an alternative.

  • Like 1
Link to comment
Share on other sites

  • Solution
5 hours ago, gizmola said:

$data is an array with one element that is an array.  That array has a lot of elements including the one you say you want.  So try:

<?php 

foreach ($data[0]['lastData'] as $key => $key_value) { 
   echo "key: $key, value: $key_value <br>";
}

Once you have figured out which keys you actually want, you should be able to access them more directly.  So for example:

 

echo "<p>Temperature (F): {$data[0]['lastData']['tempinf']}</p>";
echo "<p>Wind (mph): {$data[0]['lastData']['windspeedmph']}</p>";

 

I don't see a min/max temp value in the data you provided. 

Aside from 'lastData', there are several other keys and nested arrays in some cases.  For figuring this out in the future, aside from var_dump, print_r is often a useful development/debugging function that can be used as an alternative.

I did try the print_r function I thought they were the same. I tried your suggestion and it worked!

Thank you.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.