How to Get Your Own Weather Reports

Want to show the weather on your site? For those of us located in the United States, or its possessions, it's pretty easy really. The information is all available from the U.S. National Weather Service in XML format. PHP has built-in functions for parsing XML data and parsing the weather XML from the National Weather Service is a pretty straightforward task.

It would be very easy to just parse the XML into an array and iterate through that array to output the data, but I prefer to have a bit more control over the output. In particular, there is a significant amount of duplication in the NWS XML. It's done that way on purpose to allow the you to use data formatted in different ways. While there is some flexibility in the data, I like to take even more control and operate on the returned data even further. Here's an example of what the XML data might look like.

Getting the Data

The first step in this will be, of course, to get the XML file from the NWS. If your server has url fopen wrappers enabled, that can be as easy as using PHP's file_get_contents() function. Web hosts, however, are discovering just how naive some developers are and are turning that option off in their PHP configuration. My approach uses PHP's cURL library.

Okay, now you saw the word "complexity" and you're getting nervous. Don't. I've provided all the code you need. You can download the code and just include it in your page. All you need to do is assign the value of the four character identifier of the weather reporting station to a variable named $rpt before the include statement. The code to do that might look like this:

<?php
$rpt="KOJC";
require_once($_SERVER['DOCUMENT_ROOT']."/includes/getweather.php");
?>

The above code assumes that the getweather.php file is stored in a directory named "includes" located in your site's root directory. If you store the file somewhere else, adjust the path as necessary.

Outputting the Weather Data

After having done the above, the weather information will be contained in an associative array assigned to the variable $wxdata. The array will look like this. Like I said, there is a lot of duplication in that data. For my weather page here, I use the following array items:

Member Value in Sample File
CREDIT NOAA's National Weather Service
CREDIT_URL http://weather.gov/
URL http://weather.gov/images/xml_logo.gif
LOCATION Olathe, Johnson County Executive Airport, KS
OBSERVATION_TIME Wednesday May 18, 2005 8:53 AM CDT
ICON http://weather.gov/weather/images/fcicons/skc.jpg
WEATHER Fair and Breezy
VISIBILITY 8 miles
TEMP_F 68
TEMP_C 20
DEWPOINT_F 55
DEWPOINT_C 13
RELATIVE_HUMIDITY 63
WIND_STRING From the South at 18 gusting to 26 mph
(190 at 16 gusting to 22 knots)
PRESSURE_STRING 29.78" (1007.4 mb)
HEAT_INDEX_STRING 75 F (24 C)
HEAT_INDEX_F 75
HEAT_INDEX_C 24
WINDCHILL_STRING Not Applicable
WINDCHILL_F Not Applicable
WINDCHILL_C Not Applicable
TWO_DAY_HISTORY_URL http://www.weather.gov/data/obhistory/KOJC.html

It's a fairly simple matter then to output, for example, the temperature as something like:

The current temperature is <?php echo $wxdata['TEMP_F']."&deg;";?>.

The result would look like this, using the sample data:

The current temperature is 68°.

That's about all there is to it. Enjoy!