Get weather from BBC RSS feed using Codeigniter
Create a file called xml.php and save it within application > libaries. Add this content to it:
<?php
if ( ! defined(‘BASEPATH’)) exit(‘No direct script access allowed’); // Class xml by Troy Wolf
// http://www.troywolf.com/articles/php/class_xml/
class Xml {
var $log;
var $data;
var $parser;
var $stack;
var $index;
function xml() {
$this->log = “New xml() object instantiated.<br />\n”;
}
function fetch($raw_xml) {
$this->log .= “fetch() called.<br />\n”;
$this->index = 0;
$this->data = null;
$this->stack = array();
$this->stack[] = &$this->data;
$this->parser = xml_parser_create (“UTF-8″);
xml_set_object($this->parser,$this);
xml_set_element_handler($this->parser, “tag_open”, “tag_close”);
xml_set_character_data_handler($this->parser, “cdata”);
xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, “UTF-8″);
if (!$parsed_xml = xml_parse($this->parser,$raw_xml, true )) {
$this->log .= sprintf(“XML error: %s at line %d”,
xml_error_string(xml_get_error_code($this->parser)),
xml_get_current_line_number($this->parser));
return false;
}
xml_parser_free($this->parser);
return true;
}
function tag_open($parser, $tag, $attrs) {
$tag = str_replace(“-”, “_”, $tag);
$tag = str_replace(“:”, “_”, $tag);
foreach($attrs as $key => $val) {
$key = str_replace(“-”, “_”, $key);
$key = str_replace(“:”, “_”, $key);
$value = $this->clean($val);
$object->_attr->$key = $value;
}
$temp = &$this->stack[$this->index]->$tag;
$temp[] = &$object;
$size = sizeof($temp);
$this->stack[] = &$temp[$size-1];
$this->index++;
}
function tag_close($parser, $tag) {
array_pop($this->stack);
$this->index–;
}
function cdata($parser, $data) {
if(trim($data)){
$this->stack[$this->index]->_text .= $data;
}
}
function clean($string){
return utf8_decode(trim($string));
}
}
/* End of file xml.php */
Next, create a file called weather.php and save that within application > libaries. Add this content:
<?php
if ( ! defined(‘BASEPATH’)) exit(‘No direct script access allowed’);
class Weather {
var $source;
var $x;
var $parser;
var $stack;
var $index;
public function weather($code = 24, $location=”Norwich”) {
$x = new xml();
$source = file_get_contents(“http://newsrss.bbc.co.uk/weather/forecast/” . $code . “/ObservationsRSS.xml”);
if (!$x->fetch($source)) {
// The class has a ‘log’ property that contains a log of events. This log is useful for testing and debugging.
// Remove the <h2> once going live to prevent visible errors.
// echo “<h2>There was a problem parsing your XML!</h2>”;
echo $x->log;
exit();
}
foreach ($x->data->RSS[0]->CHANNEL[0]->ITEM as $item) {
if (preg_match(“/cloud/”, $item->TITLE[0]->_text)) {
// cloudy
$weather = ‘<h6 id=weather>Weather in ‘. $location . ‘ today: <img src=”images/weather_icons/cloudy.gif” alt=”cloudy” width=31 height=31></h6>’;
} elseif (preg_match(“/sunny intervals/”, $item->TITLE[0]->_text)) {
// sunny intervals
$weather = ‘<h6 id=weather>Weather in ‘. $location . ‘ today: <img src=”images/weather_icons/sunny-intervals.gif” alt=”sunny intervals” width=31 height=31></h6>’;
} elseif (preg_match(“/sunny/”, $item->TITLE[0]->_text)) {
// sunny
$weather = ‘<h6 id=weather>Weather in ‘. $location . ‘ today: <img src=”images/weather_icons/sunny.gif” alt=”sunny” width=31 height=31></h6>’;
} elseif (preg_match(“/mist/”, $item->TITLE[0]->_text)) {
// mist
$weather = ‘<h6 id=weather>Weather in ‘. $location . ‘ today: <img src=”images/weather_icons/rain.gif” alt=”mist” width=31 height=31></h6>’;
} elseif (preg_match(“/drizzle/”, $item->TITLE[0]->_text)) {
// drizzle
$weather = ‘<h6 id=weather>Weather in ‘. $location . ‘ today: <img src=”images/weather_icons/rain.gif” alt=”drizzle” width=31 height=31></h6>’;
} elseif (preg_match(“/light rain/”, $item->TITLE[0]->_text)) {
// light rain
$weather = ‘<h6 id=weather>Weather in ‘. $location . ‘ today: <img src=”images/weather_icons/rain.gif” alt=”light rain” width=31 height=31></h6>’;
} elseif (preg_match(“/heavy rain/”, $item->TITLE[0]->_text)) {
// heavy rain
$weather = ‘<h6 id=weather>Weather in ‘. $location . ‘ today: <img src=”images/weather_icons/rain.gif” alt=”heavy rain” width=31 height=31></h6>’;
} elseif (preg_match(“/sleet/”, $item->TITLE[0]->_text)) {
// sleet
$weather = ‘<h6 id=weather>Weather in ‘. $location . ‘ today: <img src=”images/weather_icons/snow.gif” alt=”sleet” width=31 height=31></h6>’;
} elseif (preg_match(“/snow/”, $item->TITLE[0]->_text)) {
// snow
$weather = ‘<h6 id=weather>Weather in ‘. $location . ‘ today: <img src=”images/weather_icons/snow.gif” alt=”snow” width=31 height=31></h6>’;
} else {
// $weather = ‘<h6 id=noweather>Weather feed error</h6>’;
$weather = ”;
}
}
return $weather;
}
/* End of file weather.php */
That's your libraries sorted. In your controller, add the following to load the libraries:
$this->load->library(‘xml’); $this->load->library(‘weather’);
and then get the weather by using :
$html['weather'] = $this->weather->weather(“24″, “Norwich”);
For locations other than Norwich, UK, you'll need to change the BBC RSS weather code (24, above) and the location name.