Klipp Ohei Klipp Ohei - 1 year ago 325
JSON Question

Openweathermap: json (min and max temp)

I want to implement a weather module for my website. For this I chose "Openweathermap".

I want to get the min and max temperature for today and tomorrow.


$json_string = file_get_contents("http://api.openweathermap.org/data/2.5/forecast/daily?q=london&mode=json");
$jsonData = json_decode($json_string, true);
$min_1 = $jsonData['list'][0]['temp'][0]['min'];
$max_1 = $jsonData['list'][0]['temp'][0]['max'];
$min_2 = $jsonData['list'][1]['temp'][0]['min'];
$max_2 = $jsonData['list'][1]['temp'][0]['max'];
echo $min_1.' - '.$max_1.'<br><br>';
echo $min_2.' - '.$max_2.'<br><br>';

But with this code I get no output (except the two "-").

json file

enter image description here

Answer Source

You're placing an extra [0]:

//                                   V removed [0], temp doesn't have another array in an array
$min_1 = $jsonData['list'][0]['temp']['min'];
$max_1 = $jsonData['list'][0]['temp']['max'];
$min_2 = $jsonData['list'][1]['temp']['min'];
$max_2 = $jsonData['list'][1]['temp']['max'];
echo $min_1.' - '.$max_1.'<br><br>';
echo $min_2.' - '.$max_2.'<br><br>';

To get Celsius, attach &units=metric:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download