Componente CakePHP para Yahoo! Weather

En base a lo que estuve desarrollando en las notas anteriores, hoy les voy a dejar un componente CakePHP para consumir de forma transparente el servicio RSS de Yahoo! Weather.

El componente lo pueden bajar en formato zip desde aquí.

Si bien ya existe un componente para CakePHP que se ocupa de esto, la idea era cerrar un poco el tema que estuve desarrollando con algo funcional y útil basado en el código mostrado, ya que el mencionado componente hace un planteo diferente del tema (no usa SimpleXML, está basado en expresiones regulares).

Y este es el código:


<?php
/**
* YweatherComponent
*
* Yahoo! Weather's RSS CakePHP Component
*
* @author Leonel Quinteros
*/


class YweatherComponent extends Object {

var $locationId; // Location ID

/*
* RSS weather information
*/
var $title; // RSS title
var $description; // RSS CDATA HTML current conditions.

/*
* yweather namespace information arrays.
*/
var $condition = array();
var $location = array();
var $units = array();
var $wind = array();
var $atmosphere = array();
var $astronomy = array();


/**
* startup
* CakePHP component's constructor-like method
*/
function startup(& $controller) {
$this->controller = & $controller;
}

/**
* loadXml
*
* Main/unique method. Loads RSS information into object's arrays properties.
*
* @author Leonel Quinteros
*
* @param locationId Yahoo's location ID
* @param units Yahoo's temp units ('c' or 'f'). Default 'c'
*
* @return (bool) False if there are errors loading the XML information. True if everything is ok.
*/
function loadXml($locationId, $units = 'c') {
$this->locationId = $locationId;

$url = "http://weather.yahooapis.com/forecastrss?p=$locationId&u=$units";
@$rss = file_get_contents($url);
@$yWeather = simplexml_load_string(str_replace('yweather:', '', $rss));

if($yWeather) {
/*
* Load RSS information
*/
$this->title = $yWeather->channel->item->title;
$this->description = $yWeather->channel->item->description;

/*
* Load 'yweather' namespace information.
*/
// Conditions
foreach($yWeather->channel->item->condition->attributes() as $a => $b) {
$cleanStr = array('"' => '', '=' => '', $a => '');
$this->condition[$a] = trim(strtr($b->asXML(), $cleanStr));
}

// Location
foreach($yWeather->channel->location->attributes() as $a => $b) {
$cleanStr = array('"' => '', '=' => '', $a => '');
$this->location[$a] = trim(strtr($b->asXML(), $cleanStr));
}

// Units
foreach($yWeather->channel->units->attributes() as $a => $b) {
$cleanStr = array('"' => '', '=' => '', $a => '');
$this->units[$a] = trim(strtr($b->asXML(), $cleanStr));
}

// Wind
foreach($yWeather->channel->wind->attributes() as $a => $b) {
$cleanStr = array('"' => '', '=' => '', $a => '');
$this->wind[$a] = trim(strtr($b->asXML(), $cleanStr));
}

// Atmosphere
foreach($yWeather->channel->atmosphere->attributes() as $a => $b) {
$cleanStr = array('"' => '', '=' => '', $a => '');
$this->atmosphere[$a] = trim(strtr($b->asXML(), $cleanStr));
}

// Astronomy
foreach($yWeather->channel->astronomy->attributes() as $a => $b) {
$cleanStr = array('"' => '', '=' => '', $a => '');
$this->astronomy[$a] = trim(strtr($b->asXML(), $cleanStr));
}

return true;

} else {
return false;
}

} // function loadXml

} // class YweatherComponent

?>

Descargar componente

Una vez que tenemos instalado el componente, lo utilizamos desde nuestro controlador de la siguiente manera:


class DefaultController extends AppController {
var $name = "Default";

// Declaro el componente
var $components = array('yweather');

// Accion. Cargo la info del componente y le paso los arrays que me interesan a la vista
public function weather() {
$this->yweather->loadXml('ARBA0009');
$this->set('yweatherCondition', $this->yweather->condition);
$this->set('yweatherWind', $this->yweather->wind);
}

Luego el codigo de la vista se vuelve obvio.

Como vemos, seguimos con el código de los ejemplos de las notas anteriores (Consumiendo Yahoo! Weather desde PHP, parte 1 y 2), pero ahora implementado dentro de un componente para CakePHP y extendido para consumir la totalidad de la información útil del RSS.

Hasta la próxima.