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.

8 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,

  2. Jorge says:

    Good instructions, Faruque. I am following your examples and I was able to imitate it exactly except for. How would I be able to access the json object “name” in the below Json string

    “coord”: {
    “lon”: -0.13,
    “lat”: 51.51
    “weather”: [
    “id”: 802,
    “main”: “Clouds”,
    “description”: “scattered clouds”,
    “icon”: “03d”
    “base”: “stations”,
    “main”: {
    “temp”: 15.19,
    “pressure”: 1017,
    “humidity”: 63,
    “temp_min”: 13,
    “temp_max”: 17.78
    “visibility”: 10000,
    “wind”: {
    “speed”: 3.6,
    “deg”: 280
    “clouds”: {
    “all”: 26
    “dt”: 1567327296,
    “sys”: {
    “type”: 1,
    “id”: 1414,
    “message”: 0.0096,
    “country”: “GB”,
    “sunrise”: 1567314759,
    “sunset”: 1567363746
    “timezone”: 3600,
    “id”: 2643743,
    “name”: “London”,
    “cod”: 200

    I am using the following code and I have tried but I am unable to reproduce it.

    $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;
    $country = $jsondata->sys->country;
    //$city = $jsondata->sys->name;

    Thank you in advance for your assistance.

  3. Jorge says:

    Never Mind, Faruque. I just got it and below is what I did. Thank you.

    $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;
    $country = $jsondata->sys->country;
    $city = $jsondata->name;

  4. Darren Beckford says:

    Thanks a lot for this!

  5. Alan says:

    Thanks for the heads-up on basic parsing of the OpenWeather Map feed.

    The Forecast is far more complex as it contains at least 4 or 5 entries for each Day. How should this issue be handled?

    Thanks again.

  6. Emma Palmer says:

    Hi, I am trying to get the 5 day weather forecast to work, I have put my api key in and used the code but nothing is being printed. I have tried echoing $desc and nothing appears.

  7. Shailesh Dhandhukiya says:

    how can I get 7 day forecast weather data

Leave a Reply

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