Este es mi primer aporte, y hace tiempo estaba buscando como poner el tiempo en mi web, les dejo el script completo con la api de google weather. saludos.
Fuente:
http://www.webintenta.com/clase-para...e-weather.html
Código PHP:
Ver original<html>
<head>
<title>Api de google</title>
<style type="text/css">
.pronostico{width: 170px;height: 45px;background: #000 no-repeat;background-position: right;float:left;}
.temperatura{width: 345px; height: 45px;background: #91ab0b;border-bottom: solid 3px #313131;}
.imgtem{width:30px;height:45px;float:left;}
.imgtem img{margin: 7px auto;border: solid 2px gray;}
.protem{width:140px;height:45px;float:left; font-size:11px;}
.cguinda{text-align:left;color:#eb0686;float:left;margin-left: 1px;}
.cverde{text-align:left;color:#91ab0b;float:left;margin-left: 1px;}
.cceleste{text-align:left;color:#009ee0;float:left;margin-left: 1px;}
.cblanco{text-align:left;color: white;float:left;margin-left: 2px;}
</style>
</head>
<body>
<?php
class GoogleWeatherAPI {
private $city_code = '';
private $city = '';
private $domain = 'www.google.com';
private $prefix_images = '';
private $current_conditions = array(); private $forecast_conditions = array(); private $is_found = true;
/**
* Class constructor
* @param $city_code is the label of the city
* @param $lang the lang of the return weather labels
* @return ...
*/
function __construct ($city_code,$lang='fr') {
$this->city_code = $city_code;
$this->prefix_images = 'http://'.$this->domain;
$this->url = 'http://'.$this->domain.'/ig/api?weather='.urlencode($this->city_code).'&hl='.$lang;
if(!isset($xml->weather->problem_cause)) {
$this->city = (string)$xml->weather->forecast_information->city->attributes()->data;
$this->current_conditions['condition'] = (string)$xml->weather->current_conditions->condition->attributes()->data;
$this->current_conditions['temp_f'] = (string)$xml->weather->current_conditions->temp_f->attributes()->data;
$this->current_conditions['temp_c'] = (string)$xml->weather->current_conditions->temp_c->attributes()->data;
$this->current_conditions['humidity'] = (string)$xml->weather->current_conditions->humidity->attributes()->data;
$this->current_conditions['icon'] = $this->prefix_images.(string)$xml->weather->current_conditions->icon->attributes()->data;
$this->current_conditions['wind_condition'] = (string)$xml->weather->current_conditions->wind_condition->attributes()->data;
foreach($xml->weather->forecast_conditions as $this->forecast_conditions_value) {
$this->forecast_conditions_temp = array(); $this->forecast_conditions_temp['day_of_week'] = (string)$this->forecast_conditions_value->day_of_week->attributes()->data;
$this->forecast_conditions_temp['low'] = (string)$this->forecast_conditions_value->low->attributes()->data;
$this->forecast_conditions_temp['high'] = (string)$this->forecast_conditions_value->high->attributes()->data;
$this->forecast_conditions_temp['icon'] = $this->prefix_images.(string)$this->forecast_conditions_value->icon->attributes()->data;
$this->forecast_conditions_temp['condition'] = (string)$this->forecast_conditions_value->condition->attributes()->data;
$this->forecast_conditions []= $this->forecast_conditions_temp;
}
} else {
$this->is_found = false;
}
}
function getCity() {
return $this->city;
}
function getCurrent() {
return $this->current_conditions;
}
function getForecast() {
return $this->forecast_conditions;
}
function isFound() {
return $this->is_found;
}
}
$gweather = new GoogleWeatherAPI('comitan de dominguez,chiapas','es');
if($gweather->isFound()) {
$contenido=$gweather->getCurrent();
$pronostico=$gweather->getForecast();
}
?>
<div class="temperatura">
<div class="pronostico"><div class="imgtem"><img src='<?php echo $contenido['icon'];?>' width='24'/></div><div class="protem">
<div class="cblanco">Hoy:</div><div class="cverde"><?php echo $contenido['temp_c']."º" ; ?></div><div class="cblanco">Min:</div><div class="cverde"><?php echo $pronostico['0']['low']."º"; ?></div><div class="cblanco">Max:</div><div class="cguinda"><?php echo $pronostico['0']['high']."º"; ?></div><br />
<div class="cblanco"></div><div class="cverde"><?php echo $contenido['condition']; ?></div><br />
<div class="cblanco"><?php echo $pronostico['0']['condition']; ?></div><br />
</div></div>
<div class="pronostico"><div class="imgtem"><img src='<?php echo $pronostico['1']['icon'];?>' width='24'/></div><div class="protem">
<div class="cblanco">Mañana</div><div class="cblanco">Min:</div><div class="cverde"><?php echo $pronostico['1']['low']."º"; ?></div><div class="cblanco">Max:</div><div class="cguinda"><?php echo $pronostico['1']['high']."º"; ?></div><br />
<div class="cverde"><?php echo $pronostico['1']['condition']; ?></div><br />
</div></div>
</div>
</body>
</html>
Aqui puedes cambiar por tu ciudad o codigo postal de la misma, la terminacion es, significa que se esta utilizando el idioma español, lo puedes modificar por cualquier idioma que soporte la api
Código PHP:
Ver original$gweather = new GoogleWeatherAPI('comitan de dominguez,chiapas','es');
En esta parte solamente se recogen los valores si se han encontrado, y los almacena en una variabla donde
a)$contenido, es una arreglo de una dimencion asociativo
b)$pronostico, es un arreglo bidimencional asociativo
Código PHP:
Ver originalif($gweather->isFound()) {
$contenido=$gweather->getCurrent();
$pronostico=$gweather->getForecast();
}