Interesante clase para acceder de un modo sencillo al servicio metereológico de Google.
Ejemplo:
- <?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;
-
- $content = utf8_encode(file_get_contents($this->url));
-
- $xml = simplexml_load_string($content);
-
- if(!isset($xml->weather->problem_cause)) {
-
- $xml = simplexml_load_string($content);
-
- $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('valencia','es');
- if($gweather->isFound()) {
- echo '<pre>'; print_r($gweather->getCity()); echo '</pre>';
- echo '<pre>'; print_r($gweather->getCurrent()); echo '</pre>';
- echo '<pre>'; print_r($gweather->getForecast()); echo '</pre>';
- }
- ?>
<?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;
$content = utf8_encode(file_get_contents($this->url));
$xml = simplexml_load_string($content);
if(!isset($xml->weather->problem_cause)) {
$xml = simplexml_load_string($content);
$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('valencia','es');
if($gweather->isFound()) {
echo '<pre>'; print_r($gweather->getCity()); echo '</pre>';
echo '<pre>'; print_r($gweather->getCurrent()); echo '</pre>';
echo '<pre>'; print_r($gweather->getForecast()); echo '</pre>';
}
?>
Ver ejemplo en funcionamiento » »
Google Weather API » »
No hay comentarios:
Publicar un comentario