How to Weather with PHP & openweathermap in 3 easy steps

Tuesday 15th, Dec, 2015 | #PHP

Demo

OpenWeatherMap service is developed by team of IT specialists who have 10+ year expertise in development and applying of telecommunication solutions, experience in development and utilisation of Big Data technologies in practice, and many others.

Ideology of the service is inspired by OpenStreetMap and Wikipedia that make information free and available for everybody. Today the service delivers 1 billion forecasts per day. It provides open weather data for more than 200,000 cities and any geo location via our web-site and API.

Step 1 - Get API Key

To access the API you need to sign up for an API key http://openweathermap.org

Step 2 - Locate ID City

You need get city ID from http://bulk.openweathermap.org/sample/city.list.json.gz

Step 3 - The Code

 <?php

//units=For temperature in Celsius use units=metric
//5128638 is new york ID

$url = "http://api.openweathermap.org/data/2.5/weather?id=5128638&lang=en&units=metric&APPID={APIKEY}";


$contents = file_get_contents($url);
$clima=json_decode($contents);

$temp_max=$clima->main->temp_max;
$temp_min=$clima->main->temp_min;
$icon=$clima->weather[0]->icon.".png";
//how get today date time PHP :P
$today = date("F j, Y, g:i a");
$cityname = $clima->name; 

echo $cityname . " - " .$today . "<br>";
echo "Temp Max: " . $temp_max ."&deg;C<br>";
echo "Temp Min: " . $temp_min ."&deg;C<br>";
echo "<img src='http://openweathermap.org/img/w/" . $icon ."'/ >";

?>