--- /dev/null
+<?php
+/**
+ * Weather.php
+ *
+ * PHP version 5
+ *
+ * @category Toolkit
+ * @package Weather
+ * @author Steve Sutton <steve@gaslightmedia.com>
+ * @copyright 2008 Gaslight Media
+ * @license http://www.gaslightmedia.com/license.txt The Gaslight License
+ * @version CVS: $Id: Weather.php,v 1.7 2010/08/16 17:41:03 jamie Exp $
+ * @link <>
+ */
+
+/**
+ * Weather class for Gaslight Media
+ *
+ * You can get the stationId for weather at
+ * http://www.weather.gov/xml/current_obs/seek.php?state=mi
+ * Uses NOAA for getting the rss feed for weather
+ *
+ * @category Toolkit
+ * @package Weather
+ * @author Steve Sutton <steve@gaslightmedia.com>
+ * @copyright 2008 Gaslight Media
+ * @license http://www.gaslightmedia.com/license.txt The Gaslight License
+ * @link <>
+ */
+class Toolkit_Weather
+{
+ //{{{ __Class Properties
+
+
+ /**
+ * The xml file used for caching the feed
+ * @var $_weatherFile string
+ * @access private
+ */
+ private $_weatherFile;
+
+ /**
+ * Station identifier for the feed
+ * @link http://w1.weather.gov/xml/current_obs/seek.php?state=mi
+ *
+ * @var $_stationId string
+ * @access private
+ */
+ private $_stationId = 'KCMX';
+
+
+ /**
+ * XMLReader object for parsing feed
+ * @var $_xml object
+ * @access private
+ */
+ private $_xml;
+
+ /**
+ * Description for private
+ * @var $_assoc array
+ * @access private
+ */
+ private $_assoc;
+
+ /**
+ * Storage array for the current conditions
+ * @var $currendCond array
+ * @access public
+ */
+ public $currentCond;
+
+ /**
+ * Boolean if the file exists or not
+ * @var $_cacheFileExist boolean
+ * @access private
+ */
+ private $_cacheFileExist = false;
+
+
+ /**
+ * Interger for the number of min before reloading file
+ * @var int
+ * @access private
+ */
+ private $_interval;
+ //}}}
+
+ //{{{ __construct()
+
+ /**
+ * Class Constructer
+ *
+ * @return void
+ * @access public
+ */
+ function __construct()
+ {
+ $currentErrorMask = ini_get('error_reporting');
+ error_reporting(0);
+ $this->_weatherFile = BASE.'weather-feed.xml';
+ $this->_interval = 25; // interval in minutes for page cache
+ $this->fetchWeather();
+ $this->setCurrentCond();
+ error_reporting($currentErrorMask);
+ }
+
+ //}}}
+ //{{{ fetchWeather()
+
+
+ /**
+ * fetchWeather
+ *
+ * THis will use curl to grab the weather feed using
+ * the $_weatherFile as destination
+ * and the $_stationId for the xml file name
+ *
+ * @return void
+ * @access public
+ */
+ function fetchWeather()
+ {
+ if (is_file($this->_weatherFile)) {
+ $timeintervalago = mktime(
+ date('H'),
+ date('i') - $this->_interval,
+ date('s'),
+ date('m'),
+ date('d'),
+ date('Y')
+ );
+ $file_time = filemtime($this->_weatherFile);
+ if ($file_time < $timeintervalago) {
+ $this->grabFeedUrl();
+ }
+ if (is_file($this->_weatherFile)) {
+ $this->_cacheFileExist = true;
+ }
+ } else {
+ $this->grabFeedUrl();
+ if (is_file($this->_weatherFile)) {
+ $this->_cacheFileExist = true;
+ }
+ }
+ }
+
+ //}}}
+ // {{{ grabFeedUrl()
+
+
+ /**
+ * grabFeedUrl
+ *
+ * @return void
+ * @access public
+ */
+ function grabFeedUrl()
+ {
+ // the NOAA xml feed url to be used
+ // from the _stationId
+ $feed_url = 'http://w1.weather.gov/xml/current_obs/'
+ .$this->_stationId.'.xml';
+ // Curl Options used for connection
+ $curlOptions = array(
+ CURLOPT_URL => $feed_url,
+ CURLOPT_HEADER => 0,
+ CURLOPT_RETURNTRANSFER => 1,
+ CURLOPT_TIMEOUT => 15,
+ CURLOPT_CONNECTTIMEOUT => 5,
+ CURLOPT_FAILONERROR => 1,
+ );
+ // start curl object
+ $ch = curl_init();
+ // set options
+ curl_setopt_array($ch, $curlOptions);
+ // assign the response to a string variable
+ $response = curl_exec($ch);
+ // grab the connection info to check for http_code = 200
+ $info = curl_getinfo($ch);
+ curl_close($ch);
+ if ($info['http_code'] == '200') {
+ file_put_contents($this->_weatherFile, $response);
+ $oldmask = umask(0);
+ chmod($this->_weatherFile, 0666);
+ umask($oldmask);
+ } else {
+ @touch($this->_weatherFile);
+ }
+ }
+
+
+ // }}}
+ //{{{ setCurrentCond()
+
+ /**
+ * Set currentCond
+ *
+ * Set the currentCond clas var up with variables from the xml file
+ * from the weather feed
+ *
+ * @return void
+ * @access public
+ */
+ function setCurrentCond()
+ {
+ $xml = new XML_Unserializer();
+ $xml->unserialize($this->_weatherFile, true);
+ $this->currentCond = $xml->getUnserializedData();
+ }
+
+ //}}}
+ //{{{
+ /**
+ * Getter for _interval
+ *
+ * @return Int
+ * @access public
+ */
+ public function get_interval() {
+ return $this->_interval;
+ }
+
+ //}}}
+ //{{{
+ /**
+ * Setter for _stationId
+ *
+ * @param string $_stationId Station ID
+ *
+ * @return void
+ * @access public
+ */
+ public function set_stationId($_stationId) {
+ $this->_stationId = $_stationId;
+ }
+
+
+
+
+}
+?>