En este ejemplo utilizamos PHP para recibir el XML que nos devuelve el API Weatherbug. Procesaremos ese XML por medio de las funciones propias de PHP y una función de tratamiento de XML de un tercero, que nos facilitará mucho la vida para procesar la respuesta.
En nuestro ejemplo, vamos a crear un recuadro con la información meteorológica de la ciudad de Madrid, indicando su longitud y latitud. Recibiremos un achivo XML que contendrá los datos con el sistema métrico decimal. Para ello guardaremos en una variable los datos del archivo XML con la función file_get_contents() de la siguiente manera:
//clave-api-weatherbug
$api_code='AQUI-TU-CLAVE-API';
// obtener datos
$data = file_get_contents('http://'.$api_code.'.api.wxbug.net/getLiveWeatherRSS.aspx?ACode='.$api_code.'&lat=40.4165020941502&long=-3.70256423950195&UnitType=1&OutputType=1');
Después guardaremos la información del documento XML en un array asociativo multidimensional para poder acceder más fácilmente a los datos que luego necesitaremos. Para ello vamos a utilizar una función que hay en la página http://www.bin-co.com/php/scripts/xml2array/. Guardaremos el código de la función en un archivo a parte.
//incluimos el archivo con la funcion 'xml2array.php'
include('xml2array.php');
//guardo el archivo xml en un array
$result = xml2array($data);
Ya sólo nos queda buscar los datos del array que nos interese mostrar. Para ver la estructura del array podemos utilizar la siguiente función y así buscar los datos que nos interesan:
echo "<pre>";
print_r($result);
echo "</pre>";
Por ejemplo, vamos a buscar el tiempo actual, la temperatura, el día y la hora:
//fecha
$anio=$result['aws:weather']['aws:ob']['aws:ob-date']['aws:year_attr']['number'];
$mes=$result['aws:weather']['aws:ob']['aws:ob-date']['aws:month_attr']['number'];
$dia=$result['aws:weather']['aws:ob']['aws:ob-date']['aws:day_attr']['number'];
//hora
$hora=$result['aws:weather']['aws:ob']['aws:ob-date']['aws:hour_attr']['hour-24'];
$minuto=$result['aws:weather']['aws:ob']['aws:ob-date']['aws:minute_attr']['number'];
//tiempo
$tiempo=$result['aws:weather']['aws:ob']['aws:current-condition'];
$tiempo_ico=$result['aws:weather']['aws:ob']['aws:current-condition_attr']['icon'];
Y creamos el recuadro con la información meteorológica actual de Madrid con los datos obtenidos del array.
<div style="margin: 0px 0px 10px; width: 140px;">
<div align="center" style="font-weight: bolder;">
Tiempo de Madrid
</div>
<div >
<div align="center" style=" width: 120px;">
<?echo $dia.'/'.$mes.'/'.$anio.' '.$hora.':'.$minuto?>
<p>
<img src="<?echo $tiempo_ico?>" alt="" border="0">
<p>
<?echo $tiempo?>
<p>
<span style="font-size:28px;"><strong><?echo round($tem, 0).' '.$tem_unid?></strong></span>
</div>
</div>
</div>
Para terminar vamos a mostrar el código del ejemplo completo.
<?
//clave-api-weatherbug
$api_code='AQUI-TU-CLAVE-API';
// obtener datos
$data = file_get_contents('http://'.$api_code.'.api.wxbug.net/getLiveWeatherRSS.aspx?ACode='.$api_code.'&lat=40.4165020941502&long=-3.70256423950195&UnitType=1&OutputType=1');
//incluimos el archivo con la funcion 'xml2array.php'
include('xml2array.php');
//guardo el archivo xml en un array
$result = xml2array($data);
//fecha
$anio=$result['aws:weather']['aws:ob']['aws:ob-date']['aws:year_attr']['number'];
$mes=$result['aws:weather']['aws:ob']['aws:ob-date']['aws:month_attr']['number'];
$dia=$result['aws:weather']['aws:ob']['aws:ob-date']['aws:day_attr']['number'];
//hora
$hora=$result['aws:weather']['aws:ob']['aws:ob-date']['aws:hour_attr']['hour-24'];
$minuto=$result['aws:weather']['aws:ob']['aws:ob-date']['aws:minute_attr']['number'];
$segundo=$result['aws:weather']['aws:ob']['aws:ob-date']['aws:second_attr']['number'];
//tiempo
$tiempo=$result['aws:weather']['aws:ob']['aws:current-condition'];
$tiempo_ico=$result['aws:weather']['aws:ob']['aws:current-condition_attr']['icon'];
?>
<div style="margin: 0px 0px 10px; width: 140px;">
<div align="center" style="font-weight: bolder;">
Tiempo de Madrid
</div>
<div >
<div align="center" style=" width: 120px;">
<?echo $dia.'/'.$mes.'/'.$anio.' '.$hora.':'.$minuto?>
<p>
<img src="<?echo $tiempo_ico?>" alt="" border="0">
<p>
<?echo $tiempo?>
<p>
<span style="font-size:28px;"><strong><?echo round($tem, 0).' '.$tem_unid?></strong></span>
</div>
</div>
![]() DjMiki | Una ayuda | 18/9/09 |