Using an API (with OpenWeatherMap.org as an example)

If the organization providing the API requires it, you will need to sign up as an authorized user to gain access to the API.
For this tutorial, we will be using OpenWeatherMap.org’s API to display current weather information.


Now that we have a valid API key, we can use it to create the application to display current weather information.
To display the current weather, we use the variable q with the city in which we wish the current weather data. For this example, we’ll use Palo Alto:
http://api.openweathermap.org/data/2.5/find?q=Palo+Alto&units=imperial&type=accurate&mode=xml&APPID=api-key
Accessing this using a web browser displays the following:

To use this information in an application, we’ll need to extract this data using PHP and output it to the screen.
In this example:

The echo statements display the temperature and humidity.

And here is the resultant webpage (browser view and code view):