Clase en php para obtener la previsión del tiempo desde Yahoo

He programado una clase en php con la que se puede obtener la previsión del tiempo desde el RSS de Yahoo. Si le dais a leer mas, podreis ver el código.

Puedes descargarla desde este enlace: Clase php Weather.

weather.php


–>

<?php
/* Modifica el $zoneCode a tu poblaci?Para ello dirigete a la web http://weather.yahoo.com y busca tu poblaci?
Una vez all?accede al RSS y en la URL encontrar?el c?o en la variable pasada via $_GET llamada "p" */

/* Nota: Recuerda en la web de Yahoo, ver los datos en Celsius */
/* Nota: El idioma est?onfigurado en catal? para ponerlo en otro idioma, modificar la variable $this->lang» */

/* Joan Grau maza -> lgr@vault-16.net */
/* 28/04/2010 */

/* –>> Ejemplo de uso <<– */
/*

<?php
require_once «weather.php»;
$weather = new weather;
$weather->zoneCode = «SPXX0209»; // Recuerda a buscar tu zonecode tal como se explica arriba
$weather->parseType = «auto»; // A escoger entre (‘auto’, ‘preg’, ‘simplexml’)
$weather->lang = «cat»; // A escoger entre (‘spanish’, ‘cat’)
$weather->render();
?>

<table cellpadding=»5″ cellspacing=»5″ border=»1″>
<tr class=»fila_tr_taula»>
<td>
<b>Actual<br /></b>
<center>
<img src=»<?=$weather->todayNowImage?>» width=»70%» />
</center>
<b>Cel:</b>             <?php echo $weather->todayNowText; ?><br />
<b>Temperatura:</b>     <?php echo $weather->todayNowTemp; ?>&ordm;C<br />
<b>-&raquo; Vent:</b>     <?php echo $weather->todayWindDirection; ?><br />
<b>Velocitat:</b>         <?php echo $weather->todayWindSpeed; ?> Km/h<br />
<b>Humitat:</b>         <?php echo $weather->todayHumitat; ?>%<br />
<b>Visible:</b>         <?php echo $weather->todayVisibility; ?> Km<br />
<b>Pressi&oacute;:</b>     <?php echo $weather->todayPressure; ?> b<br />
<b>Sortida Sol:</b>     <?php echo $weather->todaySunOut; ?><br />
<b>Posta Sol:</b>         <?php echo $weather->todaySunIn; ?>
</td>
<td>
<b><?php print(ucfirst(strftime(«%A, %d %B», $weather->todayDate))); ?><br /></b>
<center>
<img src=»<?php echo $weather->todayImage; ?>» width=»70%» />
</center>
<b>Cel:</b> <?php echo $weather->todayText; ?><br />
<b>Min:</b> <?php echo $weather->todayMinTemp; ?>&ordm;C<br />
<b>Max:</b> <?php echo $weather->todayMaxTemp; ?>&ordm;C
</td>
<td>
<b><?php print(ucfirst(strftime(«%A, %d %B», $weather->tomorrowDate))); ?><br /></b>
<center>
<img src=»<?php echo $weather->tomorrowImage; ?>» width=»70%» />
</center>
<b>Cel:</b> <?php echo $weather->tomorrowText; ?><br />
<b>Min:</b> <?php echo $weather->tomorrowMinTemp; ?>&ordm;C<br />
<b>Max:</b> <?php echo $weather->tomorrowMaxTemp; ?>&ordm;C
</td>
</tr>
</table>
*/

class weather
{
private $url = «http://xml.weather.yahoo.com/forecastrss?u=c&p=»;
private $xml;
private $currentt;
private $forecast;
private $wind;
private $atmosphere;
private $sun;

public $zoneCode;
public $parseType;
public $lang;

public $todayDate, $tomorrowDate, $todayNowTemp, $todayMaxTemp, $todayMinTemp, $tomorrowMaxTemp, $tomorrowMinTemp, $todayNowText, $todayText, $tomorrowText;
public $todayWindDirection, $todayWindSpeed, $todayHumitat, $todayVisibility, $todayPressure, $todaySunOut, $todaySunIn, $todayNowCode, $todayCode, $tomorrowCode;

function render()
{
if(
$this->parseType == «auto»)
{
if(
extension_loaded(«simplexml»)) $this->parseType = «simplexml»;
else
$this->parseType = «preg»;
}

setlocale(LC_TIME, $this->lang);
$this->xml = file_get_contents($this->url . $this->zoneCode);
$this->parseData();
$this->getData();
}

function parseData()
{
if(
$this->parseType == «preg»)
{
preg_match_all(‘/<yweather:condition.*?text=»(.*?)».*?code=»(.*?)».*?temp=»(.*?)».*?date=»(.*?)»/mi’, $this->xml, $this->currentt);
preg_match_all(‘/<yweather:forecast.*?date=»(.*?)».*?low=»(.*?)».*?high=»(.*?)».*?text=»(.*?)».*?code=»(.*?)»/mi’, $this->xml, $this->forecast);
preg_match_all(‘/<yweather:wind.*?chill=»(.*?)».*?direction=»(.*?)».*?speed=»(.*?)»/mi’, $this->xml, $this->wind);
preg_match_all(‘/<yweather:atmosphere.*?humidity=»(.*?)».*?visibility=»(.*?)».*?pressure=»(.*?)».*?rising=»(.*?)»/mi’, $this->xml, $this->atmosphere);
preg_match_all(‘/<yweather:astronomy.*?sunrise=»(.*?)».*?sunset=»(.*?)»/mi’, $this->xml, $this->sun);
}
else
{
$parsed = simplexml_load_string($this->xml);

$channel = $parsed->channel;
$cspaces = $channel->getNameSpaces(true);
$condicions = $channel->children($cspaces[‘yweather’]);

$n = 1;
foreach(
$condicions->wind->attributes() as $key => $value) $this->wind[$n++] = $value;
$n = 1;
foreach(
$condicions->atmosphere->attributes() as $key => $value) $this->atmosphere[$n++] = $value;
$n = 1;
foreach(
$condicions->astronomy->attributes() as $key => $value) $this->sun[$n++] = $value;

$item = $parsed->channel->item[0];
$namespaces = $item->getNameSpaces(true);
$condicions = $item->children($namespaces[‘yweather’]);

$n = 1;
foreach(
$condicions->condition->attributes() as $key => $value) $this->currentt[$n++] = $value;
$n = 0;
foreach(
$condicions->forecast[0]->attributes() as $key => $value) $this->forecast[$n++][0] = $value;
$n = 0;
foreach(
$condicions->forecast[1]->attributes() as $key => $value) { $this->forecast[$n++][1] = $value; }
}
}

function getData()
{
$this->todayDate = strtotime($this->forecast[1][0]);
$this->tomorrowDate = strtotime($this->forecast[1][1]);

$this->todayNowTemp = $this->currentt[3];
$this->todayMaxTemp = $this->forecast[3][0];
$this->todayMinTemp = $this->forecast[2][0];
$this->tomorrowMaxTemp = $this->forecast[3][1];
$this->tomorrowMinTemp = $this->forecast[2][1];

$this->todayNowText = $this->traduirTemps($this->currentt[1]);
$this->todayText = $this->traduirTemps($this->forecast[4][0]);
$this->tomorrowText = $this->traduirTemps($this->forecast[4][1]);

$this->todayWindDirection = $this->getWindDirection($this->wind[2]);
$this->todayWindSpeed = $this->wind[3];

$this->todayHumitat = $this->atmosphere[1];
$this->todayVisibility = $this->atmosphere[2];
$this->todayPressure = $this->atmosphere[3];

$this->todaySunOut = $this->sun[1];
$this->todaySunIn = $this->sun[2];

$this->todayNowCode = $this->currentt[2];
$this->todayCode = $this->forecast[5][0];
$this->tomorrowCode = $this->forecast[5][1];

$this->todayNowImage = «data:image/png;base64,» . base64_encode(file_get_contents(‘http://l.yimg.com/a/i/us/nws/weather/gr/’ . $this->todayNowCode . ‘d.png’));
$this->todayImage = «data:image/png;base64,» . base64_encode(file_get_contents(‘http://l.yimg.com/a/i/us/nws/weather/gr/’ . $this->todayCode . ‘d.png’));
$this->tomorrowImage = «data:image/png;base64,» . base64_encode(file_get_contents(‘http://l.yimg.com/a/i/us/nws/weather/gr/’ . $this->tomorrowCode . ‘d.png’));
}

function getWindDirection($gradient)
{
if(
$gradient >= 345 || $gradient < 15)
{
$windDirection[‘cat’] = ‘Nord’;
$windDirection[‘spanish’] = ‘Norte’;
}
elseif(
$gradient >= 15 && $gradient < 75)
{
$windDirection[‘cat’] = ‘Nord Est’;
$windDirection[‘spanish’] = ‘Noreste’;
}
elseif(
$gradient >= 75 && $gradient < 105)
{
$windDirection[‘cat’] = ‘Est’;
$windDirection[‘spanish’] = ‘Este’;
}
elseif(
$gradient >= 105 && $gradient < 165)
{
$windDirection[‘cat’] = ‘Sud Est’;
$windDirection[‘spanish’] = ‘Sureste’;
}
elseif(
$gradient >= 165 && $gradient < 195)
{
$windDirection[‘cat’] = ‘Sud’;
$windDirection[‘spanish’] = ‘Sur’;
}
elseif(
$gradient >= 195 && $gradient < 255)
{
$windDirection[‘cat’] = ‘Sud Oest’;
$windDirection[‘spanish’] = ‘Suroeste’;
}
elseif(
$gradient >= 255 && $gradient < 285)
{
$windDirection[‘cat’] = ‘Oest’;
$windDirection[‘spanish’] = ‘Oeste’;
}
else
//if($gradient >= 285 && $gradient < 345)
{
$windDirection[‘cat’] = ‘Nord Oest’;
$windDirection[‘spanish’] = ‘Noroeste’;
}

return $windDirection[$this->lang];
}

function traduirTemps($text)
{
/* No est?todas las traducciones hechas. Yahoo tiene mas frases para describir el estado del cielo */
$find = array();
$replace = array();

$find[0]     = «Partly Cloudy»;
$find[1]    = «Mostly Clear»;
$find[2]    = «Mostly Sunny»;
$find[3]    = «Fair»;
$find[4]    = «PM Showers»;
$find[5]    = «Showers»;
$find[6]    = «Mostly Cloudy»;
$find[7]    = «Light Rain»;
$find[8]    = «Heavy Rain»;
$find[9]    = «Rain/Wind»;
$find[10]    = «AM Rain»;
$find[11]    = «Rain»;

$replace[‘cat’][0]    = «Parcialment ennuvolat»;
$replace[‘cat’][1]    = «Majorit&agrave;riament ser&egrave;»;
$replace[‘cat’][2]    = «Majoritari&agrave;ment assolellat»;
$replace[‘cat’][3]     = «Clar»;
$replace[‘cat’][4]     = «X&agrave;fec per la tarde»;
$replace[‘cat’][5]     = «X&agrave;fec»;
$replace[‘cat’][6]     = «Majorit&agrave;riament ennuvolat»;
$replace[‘cat’][7]     = «Pluja lleugera»;
$replace[‘cat’][8]     = «Pluja forta»;
$replace[‘cat’][9]     = «Pluja i vent»;
$replace[‘cat’][10] = «Pluja pel mat&iacute;»;
$replace[‘cat’][11] = «Pluja»;

$replace[‘spanish’][0]    = «Parcialmente nublado»;
$replace[‘spanish’][1]    = «Mayormente despejado»;
$replace[‘spanish’][2]    = «Mayormente soleado»;
$replace[‘spanish’][3]     = «Claro»;
$replace[‘spanish’][4]     = «Chaparr&oacute;n por la tarde»;
$replace[‘spanish’][5]     = «Chaparr&oacute;n»;
$replace[‘spanish’][6]     = «Mayormente nublado»;
$replace[‘spanish’][7]     = «Lluvia leve»;
$replace[‘spanish’][8]     = «Lluvia fuerte»;
$replace[‘spanish’][9]     = «Lluvia y viento»;
$replace[‘spanish’][10] = «Lluvia por la manana»;
$replace[‘spanish’][11]    = «Lluvia»;

$text = str_replace($find, $replace[$this->lang], $text);

return $text;
}
}
?>

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.