Get weather information using php

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 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:,uk&appid=b6907d289e10d714a6e88b30761fae2288b30761fae22

By Geographic Coordinats

Pass latitude `lat` and longitude `lon` of your location eg:

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.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

  $country="IN"; //Two digit country code
  //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>";

Published by Sunny

A Mechanical engineer by education and web developer by passion and profession. He lives in Lucknow, India. Currently earning his livings by hitting keyboad. ;)

Join the Conversation


  1. Hi Thank You for your information. But how can i display the weather status icon using above php example ?

    1. openweathermap NOW requires an API key as part of the request.
      Get one for “free” use at their website

    2. signup for an api key and change the url to something like this -> “,IN&units=metric&cnt=7&lang=en&appid=xxxxxxxxxxxxx” , where xxxxxxxx is your api key.

Leave a comment

Your email address will not be published. Required fields are marked *