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 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.
- Ajaxify Weather Forecast PHP Script Source Code
- PHP Class to Get Weather Data Using OpenWeatherMap API
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.
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
Thank you so much. I have got your modification request and started working on it.
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.
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;
$country = $jsondata->sys->country;
//$city = $jsondata->sys->name;
?>
Thank you in advance for your assistance.
Never Mind, Faruque. I just got it and below is what I did. Thank you.
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;
$country = $jsondata->sys->country;
$city = $jsondata->name;
?>
Thanks a lot for this!
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.
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.
how can I get 7 day forecast weather data