By on April 7, 2015

A quick Air Quality and Weather API mashup

Last week, we introduced the Breezometer Air Quality API. This week, we have a second guest post from Breezometer on how to create an Air Quality and Weather API mashup.


The goal of this post is to demonstrate how easy it is to integrate BreezoMeter’s Air Quality Data through Mashape platform. There are endlessness use cases for Air Quality data, and this is just a small example on how we used Mashape’s awesome API service.
 
In the example below, we decided to use PHP foundation of WordPress that will combine our API with the Simple weather API on Mashape. We hope that through such a mashup, it will inspire you to come up with more innovative ways to use our API.
 
Here is a picture of what you will get at the end (you can also watch it live):
 
Breezometer Air Quality API and Weather API Mashup
 

Let’s get started!

We’ve been using Mashape Unirest for PHP (Unirest is a set of lightweight HTTP libraries). The call can be used in PHP like so:
 
[snippet id=”1591″] 
The response is an object that contains a few keys. We would like to use the BreezoMeter JSON from the response, like so:
 
[snippet id=”1592″] 
When running the code above, $Data contains the attributes of BreezoMeter’s API objects as you can see below:

Breezometer API response

Breezometer API response

Interpreting the JSON

  • breezometer_aqi – BreezoMeter’s Air Quality Index (AQI) that ranges from 0 = “poor” to 100 = “excellent” with 5 categories of equal lengths (you can read more here).
  • breezometer_color – Each breezometer_aqi category has it’s own color. For example breezometer_aqi that ranges from 60 to 79 and means Fair air quality, has a gradient of green-yellow color. In the example above it’s 66 which means breezometer_color is #9cd828 (hex color).
  • country_aqi, country_color – We also provide each country standards for air quality – but we won’t get into it now.
  • random_recommendations – Health recommendations that relevant to the specific breezometer_aqi. There are different recommendations for 5 different segments: children, health sensitives (health), outdoor athletes (sport), home (inside) and general outdoor activity (outside) as you can see above.

 
To make it a bit easier and cleaner we will be creating a class that can handle the calls for us for cities and locations.
 
[snippet id=”1593″] 

In order to get us even closer to the example above, we will add two new properties to the city class, one would be name and the other background image, those will allow us to add the graphical representation you see above.
 

[snippet id=”1594″] 
We create an array of cities to allow us to not repeat ourselves too much:
 

[snippet id=”1595″] 
In the HTML we embed the php code for the loop like so:
 

[snippet id=”1596″] 
Now if you will look at the air quality & weather widget, you can see that I used breezometer_color (separates between the picture to the white background), breezometer_aqi (which is 73 out of 100 in NY) and a random_recommendations (from the health sensitives segment).

That’s all folks!

Ready to start hacking?