4 A PHP class to retrieve, parse, and organize weather data for easy
\r
9 http://pxweather.abbett.org/
\r
12 Copyright (C) 2005 Jonathan M. Abbett
\r
13 <jonathan_at_abbett_dot_org>
\r
16 This program is free software; you can redistribute it and/or modify
\r
17 it under the terms of the GNU General Public License version 2, as
\r
18 published by the Free Software Foundation.
\r
20 This program is distributed in the hope that it will be useful, but
\r
21 WITHOUT ANY WARRANTY; without even the implied warranty of
\r
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
23 General Public License for more details.
\r
25 You should have received a copy of the GNU General Public License
\r
26 along with this program; if not, write to the Free Software
\r
27 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
\r
32 PHP 4.1.0 or later, installed with cURL support
\r
36 pxWeather gives you easy access to National Weather Service forecasts
\r
37 and current conditions for integration into your PHP application. To
\r
38 improve performance and decrease load on the data source, pxWeather
\r
39 can cache weather data locally.
\r
43 The latest version will always be available at
\r
44 https://sourceforge.net/projects/pxweather/
\r
48 Many thanks go to Hans Anderson for his XMLize utility, which makes
\r
49 short work of XML parsing. XMLize, which is included in this
\r
50 distribution, can be found at http://www.hansanderson.com/php/xml/
\r
54 - Decompress your .zip or .tar.gz file.
\r
55 - Copy pxweather.inc.php to your PHP-enabled website.
\r
56 - If you use PHP5, copy xmlize-php5.inc.php to the same directory
\r
57 on your website. If you use PHP4, copy xmlize-php4.inc.php.
\r
58 - If you intend to use caching, create a new directory for cache
\r
59 files to live. Make sure it has proper read/write permissions set.
\r
61 (NOTE: by default, pxWeather will use the local subdirectory
\r
62 "cache" to read/write cache files. You may use pxWeather's
\r
63 setOption method to set a different directory.)
\r
66 Testing Your Installation:
\r
67 Test your installation with the following bit of code. This code
\r
68 assumes it's running in the same directory as pxweather/xmlize and
\r
69 that caching uses the default settings.
\r
73 require_once("pxweather.class.php");
\r
76 echo $w->getCurrent("temp.string");
\r
80 When you run it, you should see the current temperature for
\r
81 Boston, MA in degrees Farenheit and Celsius.
\r
84 Configuring pxWeather:
\r
85 All pxWeather configuration is done through the class constructor
\r
86 and the class's setOption method. The examples below assume you've
\r
87 already loaded pxweather.class.php, as above.
\r
89 To set a city other than Boston, MA, call the constructor with the
\r
94 $w = new Weather($city = "Miami");
\r
98 Sometimes, two cities (i.e. Portland, ME and Portland, OR) have the
\r
99 same name. So, in any case where there might be ambiguity, consider
\r
100 using the ICAO location code for your city. The FAA maintains a
\r
101 list of codes for the USA at
\r
102 https://pilotweb.nas.faa.gov/qryhtml/icao/USA.html
\r
107 $w = new Weather($city = "KPDX");
\r
110 $w = new Weather($city = "KPWM");
\r
114 To force pxWeather to ignore its cache, and retrieve data directly
\r
115 from the source, you can call the constructor with the $force
\r
116 variable set to true:
\r
120 $w = new Weather($city = "Austin", $force = true);
\r
124 Other, less-common settings can be changed with the setOption method.
\r
125 For example, to disable caching:
\r
129 $w = new Weather();
\r
130 $w->setOption("PXWEATHER_CACHE", false);
\r
134 Four options may be set this way:
\r
136 PXWEATHER_URL (URL)
\r
137 The URL used to retrieve XML weather data.
\r
139 PXWEATHER_CACHE (true/false)
\r
140 Enable/disable caching; enabled by default.
\r
142 PXWEATHER_CACHEFOR (number)
\r
143 Number of minutes to keep cached data; 60 by default.
\r
145 PXWEATHER_CACHEAT (path)
\r
146 The directory where cache files should be stored; 'cache' by
\r
151 pxWeather extracts four kinds of data from the XML feed:
\r
153 1. Current conditions
\r
158 Current conditions can be retrieved with the getCurrent()
\r
159 method, which takes a field name as argument. The following are
\r
162 FIELD NAME EXAMPLE VALUE
\r
163 ----------------- ---------------------
\r
165 longname Boston, MA
\r
173 observation_name KBOS (Boston, MA)
\r
174 time 5 PM EDT 24 JUN 05
\r
175 temp.string 86 F (30 C)
\r
178 dewpt.string 53 F (11 C)
\r
181 rel_hum.string 32 %
\r
183 wind.string SW at 17 knt
\r
187 pressure.string 1016.7 mb (30.03 in)
\r
190 skies mostly cloudy
\r
193 The example from above:
\r
197 require_once("pxweather.class.php");
\r
198 $w = new Weather();
\r
200 echo $w->getCurrent('temp.string');
\r
204 Almanac times (sunrise, sunset) can be retrieved with the methods
\r
205 getSunrise() and getSunset(). They take no arguments, and return
\r
206 time strings in 12-hour format (i.e. 5:08 AM).
\r
208 Forecasts include a day name, a weather code, a low or high
\r
209 temperature, and a text description of the forecast. Forecasts for
\r
210 approximately the next six days and nights can be retrieved with the
\r
211 getForecasts() method, which takes no arguments. The method returns
\r
212 an array containing all forecasts. The example below cycles through
\r
213 each forecast, translates its weather code into a readable string
\r
214 with the weatherString() method, and prints high/low temperatures
\r
219 require_once("pxweather.class.php");
\r
220 $w = new Weather();
\r
225 foreach($w->getForecasts() as $forecast)
\r
227 echo "FORECAST FOR " . $forecast['day']. ":\n";
\r
228 echo $w->weatherString($forecast['weather']) . "\n";
\r
230 if (isset($forecast['low_temp']))
\r
231 echo "Low temperature: " . $forecast['low_temp'] . "\n";
\r
233 if (isset($forecast['high_temp']))
\r
234 echo "High temperature: " . $forecast['high_temp'] . "\n";
\r
236 echo "Description: " . $forecast['text'] . "\n\n";
\r
244 Daycasts are very similar to forecasts, but are not split into
\r
245 individual day and night reports and do not have text forecast
\r
246 descriptions. An example like above:
\r
250 require_once("pxweather.class.php");
\r
251 $w = new Weather();
\r
256 foreach($w->getDaycasts() as $daycast)
\r
258 echo "DAYCAST FOR " . $daycast['day']. ":\n";
\r
259 echo $w->weatherString($daycast['weather']) . "\n";
\r
261 if (isset($daycast['low_temp']))
\r
262 echo "Low temperature: " . $daycast['low_temp'] . "\n";
\r
264 if (isset($daycast['high_temp']))
\r
265 echo "High temperature: " . $daycast['high_temp'] . "\n\n";
\r
273 If you want to inspect the entire weather data structure, use the
\r
278 require_once("pxweather.class.php");
\r
279 $w = new Weather();
\r
287 Feedback and Requests:
\r
288 SourceForge hosts a variety of forums to express your constructive
\r
292 http://sourceforge.net/tracker/?atid=753105&group_id=142667
\r
294 http://sourceforge.net/tracker/?atid=753103&group_id=142667
\r
296 http://sourceforge.net/forum/forum.php?forum_id=477711
\r
298 All feedback and requests should be channeled through those outlets,
\r
299 and not sent by e-mail.
\r
302 Thank you for using pxWeather!
\r