Get latest Update ! Subscribe Now !

How to use Air Quality OpenWeatherMap API using PHP ?


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


The following URL is used to fetch the data, we are assuming that you have your own credentials (API Key) e.g appid=d62ca415a6b61d8927e6d87b8c42


As mentioned above let's fill the URL with required data, e.g shown below:




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:



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:


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.


Now, use echo to print the data as :


Output : Click to Get Full Code