I am developing an app and website for weather forecasts and I am using a company called aerisweather which has an api and responds with json data. I'm learning to code day by day at this point, but I'm having trouble with this api response...and I'm using PHP to try to work through the array to pull out the data.
Here is a typical api request on their page
<?php
// fetch Aeris API output as a string and decode into an object
$response = file_get_contents("https://api.aerisapi.com/observations/closest?p=:auto&format=json&radius=50mi&filter=allstations&limit=1&fields=id,ob.dateTimeISO,ob.tempF,ob.dewpointF,ob.humidity,ob.windSpeedMPH,ob.windDir,ob.weather,ob.heatindexF,ob.feelslikeF&client_id=CLIENT_ID&client_secret=CLIENT_SECRET");
$json = json_decode($response);
if ($json->success == true) {
// create reference to our returned observation object
print_r($json);
}
else {
echo sprintf("An error occurred: %s", $json->error->description);
}
?>
and the response is
{
"success": true,
"error": null,
"response": [
{
"id": "MID_C3868",
"ob": {
"dateTimeISO": "2020-07-02T23:48:03-04:00",
"tempF": 73,
"dewpointF": 69,
"humidity": 86,
"windSpeedMPH": 0,
"windDir": "W",
"weather": "Clear",
"heatindexF": 73,
"feelslikeF": 73
}
}
]
}
I basically need to understand how to access those values so that I can put them into an html page and make a nice observations page.
I have tried changing everything to an array by adding ",true" after json_decode (response) and then checking var_dump and this is what I get
C:\wamp64\www\aeris\aerisobs.php:10:
array (size=3)
'success' => boolean true
'error' => null
'response' =>
array (size=5)
0 =>
array (size=2)
'id' => string 'MID_C3868' (length=9)
'ob' =>
array (size=8)
...
1 =>
array (size=2)
'id' => string 'MID_D7873' (length=9)
'ob' =>
array (size=8)
...
2 =>
array (size=2)
'id' => string 'MID_NHSLM' (length=9)
'ob' =>
array (size=8)
...
3 =>
array (size=2)
'id' => string 'PWS_KMAMETHU12' (length=14)
'ob' =>
array (size=8)
...
4 =>
array (size=2)
'id' => string 'MID_F5795' (length=9)
'ob' =>
array (size=8)
...
Please, enlighten me as to how to do this...I would be forever grateful.
My goal is to have a section on my website that would look like this
Current Temperature: 'tempF' value from the api
Dew Point: 'dewpointF' value from the api
and so on...
Thank you for any and all assistance that you may offer!
Humbly,
Justin