How To Get Current Weather Using OpenWeatherMap API in PHP

After Google Weather API being deprecated, I researched over for other available options. Another reliable option was Yahoo Weather but it was very complex to use. Even its documentation was vague and boring to read. So I waved goodbye to Yahoo Weather !! It is legitimate but as I mentioned earlier, it is very complex to use.

Finally, I came up with the right and simpler solution to get current weather data and forecast. After a bit of research, I decided that the OpenWeatherMap was the perfect solution for me. If you are reading this article, you won’t regret.

I have to admit that before writing this tutorial, I Googled hours to find something that would pace the development but every solution I found were overly complicated and cumbersome. I wanted a simple and fast code. Then I finally found OpenWeatherMap which I am explaining right now.


What OpenWeatherMap does?

  • Access current weather data of any location on Earth (over 200,000 Cities worldwide!)
  • Frequently updates current weather based on global models.
  • fetch data from more than 40,000 Weather Stations from the world.
  • Data available in JSON, XML, or HTML format.

How to Use OpenWeatherMap API?

Note: You need to sign up a Free Account to receive an API key. It takes less than a moment to signup.

Before we begin, take a minute to Free Sign Up to get OpenWeatherMap API because, without unique API key, you will not get a weather data. In fact, you don’t even get access to the code.

Example of API calls without API

api.openweathermap.org/data/2.5/weather?id=2172797

If you try to access above link, you’ll see the result as follows:

{"cod":401, "message": "Invalid API key. Please see http://openweathermap.org/faq#error401 for more info."}

If you call API with correct API key, it will display with couple of array output.

{"coord":{"lon":85.32,"lat":27.72},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"base":"stations","main":{"temp":292.15,"pressure":1017,"humidity":39,"temp_min":292.15,"temp_max":292.15},"visibility":7000,"wind":{"speed":1},"clouds":{"all":40},"dt":1482913200,"sys":{"type":1,"id":7907,"message":0.0093,"country":"NP","sunrise":1482887304,"sunset":1482924763},"id":1283240,"name":"Kathmandu","cod":200}

The correct calling of API looks something like this.

http://api.openweathermap.org/data/2.5/weather?q=Kathmandu&appid=YourAPIkey


The Code

<?php
$jsonurl = "http://api.openweathermap.org/data/2.5/weather?q=Kathmandu&appid=YourAPIKeyHere";
$json = file_get_contents($jsonurl);
$weather = json_decode($json);

$kelvin = $weather->main->temp;
$celcius = $kelvin - 273.15;
?>
<i class="fa fa-sun-o"></i> Kathmandu Weather: <strong> <?php echo $celcius; ?>° Celcius Kathmandu </strong>

In the above code, replace YourAPIKeyHere with your API Key which you will get instantly after signing up. You are ready to go now. Comment in the form below if any issue persists. I will try my best to respond you ASAP !!

Happy Weather !!