Always wanted to have your site colors reflect the weather outside? Or maybe thinking about adding weather information to your local news portal? We're going to teach you all about retrieving and using meteorological data to show users on the other side of the planet whether it's sunny or cloudy outside your window.
In order to do this, we'll use PHP's very cool Services_Weather class, freely available from PEAR. Designed to ease the task of retrieving weather data off the Internet, the module makes it easy to integrate detailed weather-tracking systems into your applications. You can retrieve the latest meteorological reports off the Internet, have PHP parse those reports and generate an array of weather data, and analyze that weather data to use on your site.
How it works
To find out the weather in a particular city, you need to first obtain the weather report for that city. Airports around the world periodically generate METAR reports, which are meteorological reports of the prevailing weather conditions, and submit these reports to the American National Weather Service (NWS). These reports are freely available off NOAA's Web site, via HTTP or FTP.
A typical METAR report (the term is a French abbreviation which loosely translates to "routine aviation meteorological report") contains detailed information on cloud cover, wind speed and direction, temperature, visibility, and pressure. Everything you need for a weather display.
The only problem? They're not exactly easy to read. Consider
the following METAR for
EGLL 201520Z 17012KT 130V210 9999 FEW025TCU SCT028 24/15 Q1011 NOSIG
Now, if you were an expert on meteorological codes, you would know that when this report was generated, Heathrow was experiencing winds of 12 knots and a temperature of 24 degrees Celsius. All this information is encoded in the report above using cryptic symbols. Here's a complete list of what each symbol means.
Notice the first block of four characters in the METAR. This
is the four-letter code identifying the location where the METAR was generated
Now since it's clear that most of the information you need for an automatic weather report is in the METAR, all you need is a way to parse it and turn those cryptic codes into normal English. Luckily, PHP makes APIs available to do this for you. The class to use is the aforementioned Services_Weather, which decodes the information in a METAR and makes it easy to understand for non-technical users.
One important note before you proceed: The data retrieved by the Services_Weather class is governed by the terms and conditions of the source from whence it was retrieved. So before you go out there and start selling your code to the highest bidder, take a close look at the license terms and data usage restrictions for both the data sources and the Services_Weather class to ensure you are in compliance. The examples in this tutorial are purely illustrative.
All that weather information has come from the one-line METAR you saw earlier. Impressive, isn't it?
The script in Listing A works by creating an instance of the
Service_Weather class, and then using the _parseWeatherData() method to
retrieve the latest meteorological report for the location EGLL,
Handling multiple cities
You can now use this basic script to obtain weather conditions on as many cities as you like. Simply create an array of city identifiers, and put the parse-and-display code in a loop that iterates over the array, as in Listing E.
You can make this look even prettier by using icons to represent the different weather states. You can either download free icons from the Web, or create your own, for temperature scales, cloud cover, wind speed and direction, and weather conditions. The sample code in Listing F demonstrates the logic.
Here, the $tempImages and $condImages arrays store image filenames for different conditions. Depending on the values in the meteorological report, the appropriate image is displayed.
I hope you now have some insight into how to integrate real-time weather reports on your Web site. Play with Services_Weather a bit, and if you do something really creative, post a message and tell us all about it!