PHP Class to Get Weather Data Using OpenWeatherMap API
OpenWeatherMap is a popular weather data API provider from where you can get the weather data for current weather and also weather forecast data for a particular place or city or town. They provide weather data in JSON and XML format.
Here in this post, I am going to give you a complete PHP class code from where you can get weather data easily from any place.
PHP class for getting weather data
Below is the given PHP class code which I have named as weather_class.php:
<?php Class APIs{ protected $owapi = "YOUR_API_KEY"; } Class Currentweather Extends APIs{ private $jsonfile; private $jsondata; private $temp; private $humidity; private $pressure; private $visibility; private $description; private $windspeed; private $winddir; private $iconcode; private $coordlon; private $coordlat; private $cityname; private $countrycode; private $sunrise; private $sunset; private $cloudsall; private function jsondata($url) { $this->jsonfile = file_get_contents($url); $this->jsondata = json_decode($this->jsonfile); return $this->jsondata; } //location for current weather public function locationbycity($city, $country) { $currenturl = "http://api.openweathermap.org/data/2.5/weather?q=". str_replace(' ', '%20', $city) .",". str_replace(' ', '%20', $country) ."&units=metric&appid=". $this->owapi .""; return $currenturl; } // Get current temperature public function temperature($url) { $this->temp = $this->jsondata($url)->main->temp; return $this->temp; } // Get current humidity public function humidity($url) { $this->humidity = $this->jsondata($url)->main->humidity; return $this->humidity; } // Get current pressure public function pressure($url) { $this->pressure = $this->jsondata($url)->main->pressure; return $this->pressure; } // Get current visibility public function visibility($url) { $this->visibility = $this->jsondata($url)->visibility; return $this->visibility; } //Get current weather description public function description($url) { $this->description = $this->jsondata($url)->weather[0]->description; return $this->description; } // Get wind speed public function windspeed($url) { $this->windspeed = $this->jsondata($url)->wind->speed; return $this->windspeed; } //Get wind direction public function winddir($url) { $this->winddir = $this->jsondata($url)->wind->deg; return $this->winddir; } //Get icon code public function iconcode($url) { $this->iconcode = $this->jsondata($url)->weather[0]->icon; return $this->iconcode; } // Get country name public function cityname($url) { $this->cityname = $this->jsondata($url)->name; return $this->cityname; } //Get city name public function countrycode($url) { $this->countrycode = $this->jsondata($url)->sys->country; return $this->countrycode; } //Get sunrise time public function sunrise($url) { $this->sunrise = $this->jsondata($url)->sys->sunrise; return $this->sunrise; } //Get sunset public function sunset($url) { $this->sunset = $this->jsondata($url)->sys->sunset; return $this->sunset; } //Get cloudsall public function cloudsall($url) { $this->cloudsall = $this->jsondata($url)->clouds->all; return $this->cloudsall; } } ?>
In the above code, you have to change “YOUR_API_KEY” into your own OpenWeatherMap API key so that it will be ready to use. You can get it from openweathermap.org.
How to use the above PHP class to get weather details?
Now I am going to show you the usage of the above PHP class. To use it you can create a PHP file where you just have to include the class. below is the demo example code:
include "weather_class.php"; if(isset($_GET['city']) && isset($_GET['country'])) { $getcity = $_GET['city']; $getcountry = $_GET['country']; $location = "'". $getcity ."', '". $getcountry ."'"; $kolkatadata = new Currentweather; //$temperature = $kolkatadata->temperature($kolkatadata->locationbycity('Kolkata', 'In')); $temperature = $kolkatadata->temperature($kolkatadata->locationbycity($getcity, $getcountry)); $humidity = $kolkatadata->humidity($kolkatadata->locationbycity($getcity, $getcountry)); $pressure = $kolkatadata->pressure($kolkatadata->locationbycity($getcity, $getcountry)); $windspeed = $kolkatadata->windspeed($kolkatadata->locationbycity($getcity, $getcountry)); $winddir = $kolkatadata->winddir($kolkatadata->locationbycity($getcity, $getcountry)); $cityname = $kolkatadata->cityname($kolkatadata->locationbycity($getcity, $getcountry)); $countrycode = $kolkatadata->countrycode($kolkatadata->locationbycity($getcity, $getcountry)); $description = $kolkatadata->description($kolkatadata->locationbycity($getcity, $getcountry)); $visibility = $kolkatadata->visibility($kolkatadata->locationbycity($getcity, $getcountry)); $sunrise = $kolkatadata->sunrise($kolkatadata->locationbycity($getcity, $getcountry)); $sunset = $kolkatadata->sunset($kolkatadata->locationbycity($getcity, $getcountry)); $cloudsall = $kolkatadata->cloudsall($kolkatadata->locationbycity($getcity, $getcountry)); } else { echo "URL value not set"; }
In the above code, we can get weather data for a particular place. Suppose you have saved the above code in a file called index.php. Then you just have to pass the location in this way via URL – index.php?city=Kolkata&country=In. Then you can display each weather data by using PHP echo.
Leave a Reply