Getting weather data from OpenWeatherMap API using PHP

By Faruque Ahamed Mollick

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

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 – http://api.openweathermap.org/data/2.5/weather?q=London,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:

{"coord":{"lon":139,"lat":35},
"sys":{"country":"JP","sunrise":1369769524,"sunset":1369821049},
"weather":[{"id":804,"main":"clouds","description":"overcast clouds","icon":"04n"}],
"main":{"temp":289.5,"humidity":89,"pressure":1013,"temp_min":287.04,"temp_max":292.04},
"wind":{"speed":7.31,"deg":187.002},
"rain":{"3h":0},
"clouds":{"all":92},
"dt":1369824698,
"id":1851632,
"name":"Shuzenji",
"cod":200}

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:

<?php
$jsonfile = file_get_contents("http://api.openweathermap.org/data/2.5/weather?q=London,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:

http://api.openweathermap.org/data/2.5/forecast/daily?q=London,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:

<?php
$urlforcast="http://api.openweathermap.org/data/2.5/forecast/daily?q=London,UK&units=metric&cnt=5&appid=[YOUR_API_KEY]";
$json=file_get_contents($urlforcast);
$data=json_decode($json,true);
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 – https://gum.co/cTYHn. 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,
    Linda

Leave a Reply

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