4 * pxWeather - PHP/XML Weather Retrieval
\r
6 * jonathan[at]abbett[dot]org
\r
12 require_once("xmlize-php5.inc.php");
\r
17 define("PXWEATHER_URL", 0); // to set $_url
\r
18 define("PXWEATHER_CACHE", 1); // to set $_cache
\r
19 define("PXWEATHER_CACHEFOR", 2); // to set $_age
\r
20 define("PXWEATHER_CACHEAT", 3); // to set $_cachepath
\r
25 // The URL from whence to retreive XML weather data
\r
26 var $_url = "http://www.weather.unisys.com/forexml.cgi";
\r
27 // If true, will use a local file cache
\r
28 var $_cache = false;
\r
29 // How many minutes to keep the cached data
\r
31 // If cache-enabled, use this directory for storing data
\r
32 var $_cachepath = "cache";
\r
37 var $_xml = null; // to store our xml array
\r
38 var $_city = "boston"; // The city we're interested in
\r
39 var $_force = false; // If true, will override the cache and fetch data fresh
\r
45 function Weather($city = "Boston", $force = false) {
\r
47 $this->_city = $city;
\r
48 $this->_force = $force;
\r
52 function setOption($option, $value) {
\r
55 case EASYWEATHER_URL:
\r
56 $this->_url = $value;
\r
58 case EASYWEATHER_CACHE:
\r
59 $this->_cache = $value;
\r
61 case EASYWEATHER_CACHEFOR:
\r
62 $this->_age = $value;
\r
64 case EASYWEATHER_CACHEAT:
\r
65 $this->_cachepath = $value;
\r
72 ** Return a pretty-printed version of the XML array
\r
74 function toString($array = null) {
\r
76 if ($array == null) {
\r
77 $array = $this->_xml;
\r
86 ** Return a specific field from the current weather report
\r
88 function getCurrent($field) {
\r
89 $o = $this->_getObservation();
\r
91 if (array_key_exists($field, $o)) {
\r
99 function getSunrise() {
\r
100 $a = $this->_getAlmanac();
\r
102 return $a["sunrise"];
\r
105 function getSunset() {
\r
106 $a = $this->_getAlmanac();
\r
108 return $a["sunset"];
\r
111 function getForecasts() {
\r
113 // need to remove a layer of separation
\r
115 $forecasts = $this->_xml["forexml"]["#"]["forecast"];
\r
117 $returnArray = array();
\r
121 foreach ($forecasts as $forecast) {
\r
122 $returnArray[$i] = $forecast["@"];
\r
126 return $returnArray;
\r
130 function getDaycasts() {
\r
132 // need to remove a layer of separation
\r
134 $daycasts = $this->_xml["forexml"]["#"]["daycast"];
\r
136 $returnArray = array();
\r
140 foreach ($daycasts as $daycast) {
\r
141 $returnArray[$i] = $daycast["@"];
\r
145 return $returnArray;
\r
149 ** Returns a weather condition string given a code (i.e 'TS')
\r
151 function weatherString($code) {
\r
155 return "Thunderstorms";
\r
161 return "Mostly cloudy";
\r
167 return "Mostly clear";
\r
170 return "Partly cloudy";
\r
189 $filename = "$this->_cachepath/weather.$this->_city.cache";
\r
191 // if not force, check cache for valid file
\r
192 if (!$this->_force && $this->_cache) {
\r
194 if (file_exists($filename)
\r
195 && filemtime($filename) > (time() - ($this->_age * 60))) {
\r
197 $file = fopen($filename, "r");
\r
198 $this->_xml = unserialize(fread($file, filesize($filename)));
\r
205 // if force, or no valid cache file, get XML fresh
\r
207 $this->_xml = $this->_getXML();
\r
209 if ($this->_cache) {
\r
210 $file = fopen($filename, "w");
\r
211 fwrite($file, serialize($this->_xml));
\r
217 function _getXML() {
\r
220 curl_setopt($ch, CURLOPT_URL, $this->_url . "?" . $this->_city);
\r
221 curl_setopt($ch, CURLOPT_HEADER, false);
\r
222 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
\r
224 #echo $this->_url . "?" . $this->_city;
227 $xml = curl_exec($ch);
\r
232 if (curl_errno($ch)) {
\r
233 trigger_error("cURL Error in Weather._getXML: " . curl_error($ch), E_WARNING);
\r
239 return xmlize($xml);
\r
243 function _getObservation() {
\r
245 return $this->_xml["forexml"]["#"]["observation"][0]["@"];
\r
249 // array with keys "sunrise" and "sunset"
\r
250 function _getAlmanac() {
\r
252 return $this->_xml["forexml"]["#"]["almanac"][0]["@"];
\r