As part of a wider project (more updates posted shortly) I am working with OpenWeatherMap’s API to obtain current and forecast weather information. This is ultimately going to be displayed on a ‘digital photo frame’ type setup.
OpenWeatherMap API offers results in both XML and JSON format. For my project I have chosen the JSON route, so using the below PHP I am grabbing the weather and outputting strings and an image based on current conditions.
This is very much a work in progress piece of code, and is part of a much larger tutorial which I will be posting up once complete. However, I thought the snippet below may prove useful for visitors just wanting a straight output from OpenWeatherMap.
NB: To use the API’s you’ll need to register for a free API ID, the one in the snippet below is just for example purposes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $url="http://api.openweathermap.org/data/2.5/forecast?q=London,usl&appid=2de143494c0b295cca9337e1e96b00e0;units=metric&cnt=7&lang=en"; $json=file_get_contents($url); $data=json_decode($json,true); echo "<img src="http://openweathermap.org/img/w/" . $data[" alt="" align="left" />"; echo "Temp: ". $data['main']['temp']."ºC "; echo $data['weather'][0]['description']." "; echo $data['weather'][0]['main']." "; echo "Wind Speed: " . $data['wind']['speed']." mph "; ?> |