Developer

Displaying weather on your Web site with PHP

Always wanted to have your site colors reflect the weather outside, or show local weather conditions on your blog? Thanks to PHP's very cool Services_Weather class, it's a cinch to make your Web site weather-smart.

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 London's HeathrowAirport:

2004/07/20 15:20
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 (HeathrowAirport is EGLL). METARs are keyed by this code, and if you plan to use these weather reports on your Web site, one of the first pieces of information you will need is the four-letter code for each city you plan to track. Luckily, it's easy to get that information too—just head here or back to the NOAA Web site for a complete list of city codes, sorted by country.

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.

Getting started

Once you've downloaded and installed Services_Weather, type out the simple script in Listing A. When you run this script, you should see something like the sample output in Listing B.

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, HeathrowAirport. The report is retrieved via anonymous FTP from the NOAA Web site, and uses the location identifier as key, so it's easy to construct a URL to it. The _parseWeatherData() method takes this URL, opens up a connection to the FTP site, reads in the report and then breaks it into its component parts using a variety of pattern-matching tricks. Each element of the report is then translated into its human-readable equivalent, and presented as elements of an associative array. By looking at the keys of the array in Listing B, you can see that the report is quite extensive and includes all the information you need.

It's now possible to use this array to create a neatly formatted report—as the rewrite in Listing C illustrates. Listing D shows what the output looks like.

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!

Editor's Picks