-
Notifications
You must be signed in to change notification settings - Fork 7.6k
weather.com
Derek Jones edited this page Jul 5, 2012
·
4 revisions
Category:Library::Community | Category:Library::Weather
<?php
/**
* Weather Script for CodeIgniter
*
* @author Kevin Burton [CoDeR]
* @package CodeIgniter 1.5.x
* @subpackage Libraries
* @version 1.1
*
* Thanks to Nick Schaffner from 53x11.com for the script.
* This has been modified and ported to CodeIgniter 1.5.4
*
* REQUIREMENTS
*
* 1. URL and DATABSE must be loaded.
* 2. Set the below paramters
* 3. By requirements of the weather.com API, you can only refresh the weather a minimum of 30 minutes.
* You can set this to more if you want by changing the cache_time value.
* 4. If you do not have the table setup already, the constructor will do it for you automatically.
* 5. You will need to register with weather.com and it will give you a folder of icons you use with
* the system. This system will output the filename for it.
* 6. ENJOY!
*
* USAGE
*
* <?
* $config['zipcode'] = 'XXXXXXX';
* $config['partner'] = 'XXXXXXX';
* $config['license'] = 'XXXXXXX';
* $this->load->library('weather', $config);
* ?>
*
* Get the array by calling: $this->weather->get_weather();
* To spit out the contents of the array, use: $this->weather->trace_array();
*
* OR
*
* You can access the variables individually instead of through the get_weather() function
* Example: $this->weather->sunr; // Sunrise.
* or $this->weather->icon; // the icon name
*
*
*/
class Weather
{
/* You must modify these variable to match your own */
var $zipcode = 'XXXXXX';
var $partner = 'XXXXXXXXX';
var $license = 'XXXXXXXXXXXXX';
var $wfile = './assets/weather/cache/weather.xml';
var $units = 'm';
var $table_name = 'weather';
var $site_name = 'local';
/* DO NOT MODIFY below: */
var $xmlserver = 'http://xoap.weather.com/weather/local/';
var $CI; // declare holder for CodeIgniter
var $cache_time = 30; // 30 minutes requied for cache
var $data_array = array();
/**
* PHP4 Constructor
*
* @return Weather
*/
function Weather($params = array())
{
$this->CI =& get_instance(); // load instance of CodeIgniter
$this->CI->load->helper('file'); // load File Helper functions
$this->initialize($params);
}
/**
* Class Initialization
*
* @access public
* @param array $params
* @return void
*/
function initialize($params = array())
{
if( count($params) > 0 )
{
foreach ($params as $key=>$value)
{
$this->$key = $value;
}
}
$this->_check_install();
$this->_setURL();
$this->_requestXML();
}
/**
* Trace method… Array and Object Friendly viewer
*
* @param unknown_type $obj
* @param unknown_type $die
*/
function trace($obj, $die=false)
{
print("");
print_r($obj);
print("</pre>");
if($die)
{
die();
}
}
/**
* Check Installation
*
* @access private
* @return void
*/
function _check_install()
{
//$this->trace($this->CI, true);
if (!$this->CI->db->table_exists($this->table_name))
{
$sqlstmnt = "
CREATE TABLE '{$this->table_name}' (
'site' varchar(64) NOT NULL default '{$this->site_name}',
'last_request' timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
'tstamp' int(16) NOT NULL default '0'
) ENGINE=MyISAM DEFAULT CHARSET=latin1;";
$this->CI->db->query($sqlstmnt);
}
}
/**
* Set URL
*
* @access private
* @return void
*/
function _setURL()
{
$this->xmlserver = $this->xmlserver.$this->zipcode."?link=xoap&cc;=*∥={$this->partner}&key;={$this->license}&unit;={$this->units}";
}
/**
* Request the XML for the weather.com server
*
* @access private
* @param unknown_type $zipcode
* @return void
*/
function _requestXML()
{
$query = $this->CI->db->query("SELECT tstamp FROM {$this->table_name} WHERE site='{$this-<site_name}' LIMIT 0,1");
if($query->num_rows()==0)
{
$this->seek(true);
return;
}
else
{
$row = $query->row_array();
if( (time()-$row['tstamp']) > (60*$this->cache_time) )
{
// get a fresh copy of the XML;
$this->seek(true);
return;
}
else
{
$this->seek(false);
return;
}
}
}
/**
* Grab the data....
*
* @access private
* @param bool $fromWeatherCom
* @return void
*/
function seek($fromWeatherCom = false)
{
$xml_parser = xml_parser_create();
if($fromWeatherCom==true)
{
$data = implode('', file($this->xmlserver));
write_file($this->wfile, $data);
}
else
{
$data = read_file($this->wfile);
}
xml_parse_into_struct($xml_parser, $data, $vals, $index);
xml_parser_free($xml_parser);
$t = true;
foreach ($vals as $row)
{
if( isset($row['value']) )
{
if( $row['level']==3 )
{
$tag = strtolower($row['tag']);
$value = $row['value'];
$this->$tag = $value;
$this->data_array[$tag] = $value;
}
}
}
$query = $this->CI->db->query("SELECT site FROM {$this->table_name} WHERE site='{$this-<site_name}'");
if( $query->num_rows()==0)
{
$this->CI->db->query("INSERT INTO {$this->table_name} (site, tstamp) VALUES ('{$this->site_name}',".time().")");
}
else
{
$this->CI->db->query("UPDATE {$this->table_name} SET tstamp='".time()."' WHERE site='{$this-<site_name}'");
}
}
/**
* Spit out the data_array
*
*/
function trace_array()
{
$this->trace($this->data_array, true);
}
/**
* Grab all the data....
*
* @return array
*/
function get_weather()
{
return $this->data_array;
}
}
?>