Getting weather data from OpenWeatherMap API using PHP

OpenWeatherMap is a weather data provider which provide the weather stats data for a particular place that may be a city or town. You need to get OpenWeatherMap API from their site

In this tutorial, I am going to tell you how to get and then display the weather data from the JSON format using PHP.

Getting current weather data from OpenWeatherMap JSON API

The current weather data for a particular place (city or town) can be found from this URL format –,UK&units=metric&appid=[YOUR_API_KEY].

The above URL will show you the current weather stats for London.

You need to put your own API in the above URL. You can get it from OpenWeatherMap, the official site. If you visit the URL, you will find JSON data that looks like below:

"weather":[{"id":804,"main":"clouds","description":"overcast clouds","icon":"04n"}],

You can see that this JSON formatted data contains all the required weather data like temperature, wind speed, humidity, pressure and many other weather-related terms. Now you need to decode that JSON formatted data. There is a PHP function json_decode which can be used to decode the JSON.

Below is the PHP code which will decode and get some weather data in PHP variable:

$jsonfile = file_get_contents(",UK&units=metric&appid=[Your_API_KEY]");

$jsondata = json_decode($jsonfile);
$temp = $jsondata->main->temp;
$pressure = $jsondata->main->pressure;
$mintemp = $jsondata->main->temp_min;
$maxtemp = $jsondata->main->temp_max;
$wind = $jsondata->wind->speed;
$humidity = $jsondata->main->humidity;
$desc = $jsondata->weather[0]->description;
$maind = $jsondata->weather[0]->main;

Keep in mind that you have to put your API key in the above code. After that, you can echo any variable.

Getting weather forecast data for upcoming days

You can get the weather forecast for upcoming days from URL that looks like below:,UK&units=metric&cnt=4&appid=[YOUR_API_KEY]

The above code will show you the weather forecast for upcoming 4 days for London. You can notice the red text “cnt=4” in the URL. This part is responsible for returning the upcoming 4 days weather forecast in JSON format. If you want 5 days weather forecast then you need to put “cnt=5” and so on.

To decode the JSON formatted weather forecast and take it inside PHP variable, below is the PHP code which will do that:

foreach($data['list'] as $day => $value) { 

  $desc = $value['weather'][0]['description'];
  $max_temp = $value['temp']['max'];
  $min_temp = $value['temp']['min'];
  $pressure = $value['pressure'];


The above code will return you weather forecast report for the upcoming 5 days.

If you want a complete nicely designed PHP weather forecast script that is built on OpenWeatherMap API then here is my own made script in this link – After download the weather forecast PHP script from here, you just need to put required API in a particular PHP file and then immediately you can use it.

2 responses to “Getting weather data from OpenWeatherMap API using PHP”

  1. Linda Rawson says:

    Hello. I would love to utilize this script and want to have some modifications done. How do I get in touch with the author?

    Thank you,

Leave a Reply

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