Weather is a PHP script to provide weather data for
cities around the world. When I was looking for such a script to integrate into
my site I couldn't find anything that suit my needs. Most of them simply combined
explode, replace and ereg statements and provided html code that was hard to
modify. The one script I did find that looked to be exactly what I wanted read
METAR reports, which, unfortunately, did not list the cities I was looking to
use. The ones that did presented the data in HTML format, and I wanted the data
itself, not the HTML. So I created this PHP class. It's simple to use and provides
all the data in variables that you can easily enter into your site in whatever
format you choose.
Getting a ton of "Notices", but otherwise everything seems to
be working? Set your PHP
error reporting to hide Notices by adding this to the start of your file:
error_reporting (E_ERROR
| E_WARNING | E_PARSE);
Weather depends on Weather.com keeping their site format rather
consistent. Lately they've been changing it occasionally. If you find Weather
isn't working for you (returning blank or invalid data) please use the test
page to identify the problem before contacting me.
With Version 2.0 and higher, Weather makes use of WebCell,
my web scrapping application, which makes it much easier to modify to meet any
new changes that Weather.com makes. As of Version 2.2, it does some data mulching
first, which should make it a little more robust. If you notice it's broken
and you're technically inclined, feel free to update it yourself (read the WebCell
information first) and send me the new version.

How to...
____________
:: STEP 1 ::
Place the following include statement in your script:
<? include("path/to/Weather.php");
?>
You will (of course) need to substitute path/to/Weather.php with
the path, i.e.: scripts/Weather.php
____________
:: STEP 2 ::
Before you can use the weather data you must fetch it. To
do so place the following line in your file before you attempt to call the data
functions:
<? $weatherData = New Weather("LOCATION_STRING");
?>
substitute LOCATION_STRING with the string from Weather.com - e.g.: Bangkok,
Thailand is "THXX0002". Find the location you want to use by going
to
Weather.com
and finding the city you want a weather report for. Look at the URL for your
LOCATION_STRING. eg: http://www.weather.com/weather/local/THXX0002
New caching options (New in Version 2.0)
If you wish to use the caching functions built into WebCell, you will
need a directory that WebCell can write to. This requires modifying permissions
on either the WebCell script, or the directory you want to use for caching.
If you don't know how to do this, ask your sysadmin.
To enable caching, call the constructor function with the optional CACHE_DIR
and CACHE_TIMEOUT values instead of just the LOCATION_STRING. eg:
<? $weatherData = New Weather("LOCATION_STRING",
"CACHE_DIR", CACHE_TIMEOUT); ?>
If you don't set at least the CACHE_DIR, caching will not be enabled.
CACHE_DIR is the complete path the the directory that WebCell has been given
access to write to. CACHE_TIMEOUT is the number of seconds that the cache
should be valid for (before reloading the page from the Weather.com servers).
It's optional, and if you don't specify a value, it defaults to 21,600 seconds
(6 hours). If you can use caching, you should do so. It will decrease the
load on your server and greatly increase page load times.
____________
:: STEP 3 ::
Now you have your weather data contained in the $weatherData
object. To use it simply call one of the data functions below using the format:
<? $currentTemperature = $weatherData->getTemp(0,"c");
?>
Which will return an integer containing the current temperature for your chosen
location.
Since these return actual variables and not HTML code you
can use them in scripts and calculate various results such as the difference
in temperature between two location for example. Note that not all locations
will report all items - Alaska is unlikely to have a Heat Index in the middle
of winter and some locations will (for various reasons) not report the latest
weather. If this occurs the function you call will return false. You may want
to check for this before relying on the results you're expecting. UPDATE: Weather.com
now reports a value "Feels Like" which is a combination of Heat Index
and Windchill. I've created a matching function getFeelsLike to return this
value. getHeatIndex and getWindChill are maintained for backward compatibility.
Most of the functions require one or two parameters, DAY and METRIC:
- DAY is the day you are looking to get data for. 0 (zero) is the current weather, 1 is today/tonight's general forecast, and 2-10 are forecasts for the next 9 days. NOTE: DAY currently does not do anything. It's there
because Weather.com reports the data and I plan to write code to read it - I just haven't done so yet.
- METRIC is the unit of measure you want the result to be returned in and is explained for each function below.
Here are the available function calls:
getLocation() - Returns a string with the location of the weather readings
- not all cities return their own weather reports
getUpdateTime(DAY) - Returns a weather.com formatted string containing
the date/time (and possibly time zone) of the last weather report.
getCondition(DAY) - Returns a string of the weather condition - e.g.:
"Fair", "Cloudy", "Raining", etc.
getImageURL(DAY) - Returns a string containing the URL for the image
provided by Weather.com - this image is a visual representation of the condition
getTemp(DAY,METRIC) - Returns a double containing the temperature - METRIC
is "c" (Celsius) or "f" (Fahrenheit)
getFeelsLike(DAY,METRIC) - Returns a double containing the temperature
as it would feel including any windchill or heat index. - METRIC is "c"
or "f". (New in Version 2.0)
getWindChill(DAY,METRIC) - Returns a double containing the temperature adjusted
for current wind-chill conditions - METRIC is "c" or
"f"
getHeatIndex(DAY,METRIC) - Returns a double containing the temperature
adjusted for current heat index conditions - METRIC is "c"
or "f"
getWindSpeed(DAY,METRIC) - Returns a double containing the wind speed
- METRIC is "mph" or "kph"
getWindDirection(DAY) - Returns a string of the current wind direction
- e.g.: "north", "southeast", etc. - Weather.com reports
the wind direction as "blowing from" - this script switches it around
so the returned string is the actual direction the wind is blowing in
getDewpoint(DAY,METRIC) - Returns a double containing the dewpoint temperature
- METRIC is "c" or "f"
getRelativeHumidity(DAY) - Returns an integer containing the relative
humidity (a percentage)
getVisibility(DAY,METRIC) - Returns a double containing the visibility
distance - METRIC is "mi" (miles) or "km"
(kilometers)
getBarometerStatus(DAY) - Returns a string containing the barometer's
status - e.g.: "rising" or "falling" - returns false if
barometer is stable
getBarometer(DAY,METRIC) - Returns a double containing the barometer
height - METRIC is "in" (inches) or "cm"
(centimeters)
outputDebug() - Outputs all the above values to the browser. If you suspect
something is wrong, call this function. If you see a lot of empty spaces (or
incorrect values) then Weather.com has probably updated their site. Let
me know what location (including LOCATION_STRING) you're having problems
with and I'll fix the script for you.

Legal
Remember that you must get permission from Weather.com in order to grab their data and use it on your site. So far as I know they have a program where you can sign up and get permission to place some of their code on your site. Whether this script falls within those terms of use guidelines or not I cannot say. I am not responsible for your use of this script or any resulting legal action. In other words, cover your butt and ask their permission.
So far as the rights on this script are concerned I give you full rights to use, distribute, and modify this script, free of charge, as long as the copyright notice at the top is maintained. If you do make changes feel free to send them to me and I'll consider including them in future releases. Since I am providing this script free of charge I also take no responsibility for any damage or losses that may occur while using this script. Use at your own risk.