Most of webmasters want to display weather information on their website. So I have decided to provide a tutorial to do this via PHP script.
There are tones of APIs that provides weather information like Yahoo weather, Open Weather Map, Accuweather, etc but my favourite is Open Weather Map. Lets start with OpenWeatherMap API.
Update: OpenWeatherMap API requires API key Since 2016. To Get free API key Visit https://openweathermap.org/appid. And use it every API calls
Get current weather forecast
By City Name
Pass city name and two digit country code in query parameter `q` eg:
api.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae2288b30761fae22
By Geographic Coordinats
Pass latitude `lat` and longitude `lon` of your location eg:
api.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=b6907d289e10d714a6e88b30761fae2288b30761fae22
Response in JSON format
{"coord":{"lon":-0.13,"lat":51.51},"sys":{"message":0.046,"country":"GB","sunrise":1402371809,"sunset":1402431395},"weather":[{"id":800,"main":"Clear","description":"Sky is Clear","icon":"01d"}],"base":"cmc stations","main":{"temp":287.58,"pressure":1016,"humidity":77,"temp_min":286.15,"temp_max":289.15},"wind":{"speed":2.6,"deg":260},"rain":{"3h":0},"clouds":{"all":0},"dt":1402377600,"id":2643743,"name":"London","cod":200}
Parameters description
Parameter | Description | Using |
---|---|---|
id | City identification | mandatory |
dt | Time of data receiving in unixtime GMT | mandatory |
coord.lat coord.lng | City location | mandatory |
name | City name | mandatory |
main.temp | Temperature in Kelvin. Subtracted 273.15 from this figure to convert to Celsius. | mandatory |
main.humidity | Humidity in % | mandatory |
main.temp_min main.temp_max | Minimum and maximum temperature | Optional |
main.pressure | Atmospheric pressure in hPa | mandatory |
wind.speed | Wind speed in mps | mandatory |
wind.deg | Wind direction in degrees (meteorological) | mandatory |
clouds.all | Cloudiness in % | mandatory |
weather | weather condition | Optional |
rain.3h | Precipitation volume mm per 3 hours | Optional |
snow.3h | Precipitation volume mm per 3 hours | Optional |
Getting weather information using php
<?php $city="Delhi"; $country="IN"; //Two digit country code $url="http://api.openweathermap.org/data/2.5/weather?q=".$city.",".$country."&units=metric&cnt=7&lang=en&appid=b6907d289e10d714a6e88b30761fae2288b30761fae22"; $json=file_get_contents($url); $data=json_decode($json,true); //Get current Temperature in Celsius echo $data['main']['temp']."<br>"; //Get weather condition echo $data['weather'][0]['main']."<br>"; //Get cloud percentage echo $data['clouds']['all']."<br>"; //Get wind speed echo $data['wind']['speed']."<br>"; ?>
Hi Thank You for your information. But how can i display the weather status icon using above php example ?
-thanks
I get the following error:
Warning: file_get_contents(http://api.openweathermap.org/data/2.5/weather?q=Delhi,IN&units=metric&cnt=7&lang=en): failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized in /opt/lampp/htdocs/weather2/index.php on line 8
What should I do?
openweathermap NOW requires an API key as part of the request.
Get one for “free” use at their website
signup for an api key and change the url to something like this -> “http://api.openweathermap.org/data/2.5/weather?q=Delhi,IN&units=metric&cnt=7&lang=en&appid=xxxxxxxxxxxxx” , where xxxxxxxx is your api key.
You should add the api key to access this url.
sample: http://api.openweathermap.org/data/2.5/weather?q=Kerala,in&APPID=your-api-key-here
To get api key :http://www.openweathermap.org/appid#get
same problem marc…same problem
use your brain lmao
i got this error during exuecution…
Warning: file_get_contents(http://api.openweathermap.org/data/2.5/weather?q=dhule,IN&units=metric&cnt=7&lang=en) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized in C:\xampp\htdocs\shreeyog\includes\header.php on line 7
Thanks all works great!