Examples of OpenWeatherMap API using PHP (Full Code)

  In, this tutorial we are going to discuss OpenWeatherMap API key, to fetch weather data from different countries.

PHP Program Code:

   
 
function WeatherUrl($url){
		$cn = curl_init();
		curl_setopt($cn, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($cn, CURLOPT_URL, $url);    // get the contents using url
		$weatherdata = curl_exec($cn); // execute the curl request
		curl_close($cn); //close the cURL
		return $weatherdata;
}

$url="http://api.openweathermap.org/data/2.5/weather?q=Delhi,IN,IN&units=metric&appid=d62ca415a6b61d8927e6d87b8c420c9f";
$response=WeatherUrl($url);




$data = json_decode($response);

	
$temp = $data->main->temp . '℃';
$minimum_temp = $data->main->temp_min . '℃';
$maximum_temp = $data->main->temp_max . '℃';
$feels_like = $data->main->feels_like . '℃';
$pressure = $data->main->pressure . '℃';
$humidity = $data->main->humidity . '℃';

echo '
Temperature : '. $temp; echo '
Minimum Temperature : '. $minimum_temp; echo '
Maximum Temperature : '. $maximum_temp; echo '
Feels Like : '. $feels_like; echo '
Pressure : '. $pressure; echo '
Humidity : '. $humidity;

Output

Temperature : 14.05℃
Minimum Temperature : 12℃
Maximum Temperature : 16.11℃
Feels Like : 12.49℃
Pressure : 1014℃
Humidity : 94℃

Leave a Reply

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