How to use Air Quality OpenWeatherMap API using PHP ?
Theory
In, this tutorial we are going to discuss Air Quality OpenWeatherMap API key, to fetch Air Quality Index data from different location as shown below :
Сoncentration of CO (Carbon monoxide), μg/m3 components.no Сoncentration of NO (Nitrogen monoxide), μg/m3 components.no2 Сoncentration of NO2 (Nitrogen dioxide), μg/m3 components.o3 Сoncentration of O3 (Ozone), μg/m3 components.so2 Сoncentration of SO2 (Sulphur dioxide), μg/m3 components.pm2_5 Сoncentration of PM2.5 (Fine particles matter), μg/m3 components.pm10 Сoncentration of PM10 (Coarse particulate matter), μg/m3 components.nh3 Сoncentration of NH3 (Ammonia), μg/m3
In order to fetch the data we need an API key, Just go to the website of OpenWeatherMap and create a new account then get the API Key.
Before making an API call please make sure you have longitude and latitude data of location to fetch the data.
e.g longitude = 77.2167 and latitude = 28.6667
Table of Contents
The following URL is used to fetch the data, we are assuming that you have your own credentials (API Key) e.g appid=d62ca415a6b61d8927e6d87b8c42
http://api.openweathermap.org/data/2.5/air_pollution?lat={lat}&lon={lon}&appid={API key}
As mentioned above let’s fill the URL with required data, e.g shown below:
http://api.openweathermap.org/data/2.5/air_pollution?lat=28.6667&lon=77.2167&lang=en&appid=d62ca415a6b61d8927e6d87b8c420c9f
The format of the data is given in JSON (default) format, When we call the it via API it will return the data in XML format.
Now, we have a working URL that helps to fetch the data using PHP, Let’s move on PHP part. First, we need to create a function to fetch the data through these URL, the function used cURL as shown below using in PHP 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;
}
Create a variable named $url, and assign the URL which created before, then call the WeatherUrl() function with argument $url to get the data as mentioned below:
$url="http://api.openweathermap.org/data/2.5/air_pollution?lat=28.6667&lon=77.2167&lang=en&appid=d62ca415a6b61d8927e6d87b8c420c9f";
$response=WeatherUrl($url);
To extract the data from json_file we need to create a JSON object, to do this we need to call the json_decode function and pass the $response.
$data = json_decode($response);
$aqi = $data->list[0]->main->aqi;
$co = $data->list[0]->components->co;
$no = $data->list[0]->components->no;
$no2 = $data->list[0]->components->no2;
$o3 = $data->list[0]->components->o3;
$so2 = $data->list[0]->components->so2;
$pm2_5 = $data->list[0]->components->pm2_5;
$pm10 = $data->list[0]->components->pm10;
$nh3 = $data->list[0]->components->nh3;
Now, use echo to print the data as :
echo '<br>Air Quality Level : '. $aqi.'<br>';
echo '<br>Co : '. $co;
echo '<br>No : '. $no;
echo '<br>NO2 : '. $no2;
echo '<br>O3 : '. $o3;
echo '<br>SO2: '. $so2;
echo '<br>PM2.5: '. $pm2_5;
echo '<br>PM10: '. $pm10;
echo '<br>NH3: '. $nh3;
Output
Air Quality Level : 5
Co : 1295.09
No : 0
NO2 : 23.65
O3 : 98.71
SO2: 16.21
PM2.5: 294.02
PM10: 324.39
NH3: 7.41
Leave a Reply