Examples of Air Quality Pollution Index API using PHP (Full Code)

  The following program will help to get the air quality pollution index from openweathermap api using php.

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/air_pollution?lat=28.6667&lon=77.2167&lang=en&appid=d62ca415a6b61d8927e6d87b8c420c9f";
$response=WeatherUrl($url);


$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;

echo '
Air Quality Level : '. $aqi.'
'; echo '
Co : '. $co; echo '
No : '. $no; echo '
NO2 : '. $no2; echo '
O3 : '. $o3; echo '
SO2: '. $so2; echo '
PM2.5: '. $pm2_5; echo '
PM10: '. $pm10; echo '
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

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