Drupal: BBC weather block
I've had so many calls about this. How can this be? It's only a simple block pulling in an RSS feed. I wonder if some people are trying to use cURL.
This module is running on this site, you can see the weather block on the right hand column.
/*
* How many jobs would you like to display in the block?
*/
define('HOW_MANY_DAYS', 5);
define('WEATHER_URL', 'http://newsrss.bbc.co.uk/weather/forecast/8/Next3DaysRSS.xml') ;
function weather_block($op = 'list', $delta = 0, $edit = array()) {
$blocks = array() ;
switch ($op) {
case 'list':
$blocks['weather']['info'] = t('Weather') ;
break ;
case 'view':
switch ($delta) {
case 'weather':
$jobs = _weather_fetch() ;
$blocks['subject'] = null ;
$blocks['content'] = theme('weather', $jobs);
}
break ;
}
return $blocks ;
}
function _weather_fetch() {
$data = @file_get_contents(WEATHER_URL);
$testfeed = @simplexml_load_string($data);
if ($testfeed !== FALSE) { // check for valid, well formed XML
unset($testfeed);
$feed = new simplexmlelement($data);
$howmany = count($feed->channel->item);
if ($howmany > HOW_MANY_DAYS) $howmany = HOW_MANY_DAYS;
for ($x = 0; $x < $howmany; $x++) {
if (strlen($feed->channel->item[$x]->title) > 0 && strlen($feed->channel->item[$x]->link) > 0) {
// $jobs[] = l($feed->channel->item[$x]->title, $feed->channel->item[$x]->link, array(), null, null, true);
$ftitle = preg_replace('/^([^:]+)/', '\\1', $feed->channel->item[$x]->title);
$jobs[] = $ftitle;
}
}
return $jobs ;
}
}
function theme_weather($jobs) {
$output = '' ;
if (count($jobs)) {
$output = theme('item_list', $jobs) ;
return $output ;
}