php:openweathermap_api_in_php_example
Documentation on the API is available from https://openweathermap.org/current
function getweather($City) { $url = "http://api.openweathermap.org/data/2.5/weather?q=".$City."&lang=en&units=metric&APPID= << Your API Key Goes here >>"; $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_VERBOSE, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $response = curl_exec($ch); curl_close($ch); $data = json_decode($response); $currentTime = time(); $html=date("l g:i a", $currentTime)." ".date("jS F, Y",$currentTime)." => "; $html.=ucwords($data->weather[0]->description)." "; /* <img src="http://openweathermap.org/img/w/<?php echo $data->weather[0]->icon; ?>.png"> */ $html.="Temperature ".$data->main->temp_min."°C to ".$data->main->temp_max."°C "; $html.="Humidity: ".$data->main->humidity." "; $html.="Wind: ".$data->wind->speed; return $html; }
This simple example returns a string of current weather for a given city, example below.
echo getweather("Nottingham,uk");
/home/gen.uk/domains/wiki.gen.uk/public_html/data/pages/php/openweathermap_api_in_php_example.txt · Last modified: 2019/05/20 10:18 by genadmin