2010-09-03

Clase para acceder a Google Weather

Interesante clase para acceder de un modo sencillo al servicio metereológico de Google.
Ejemplo:
  1. <?php  
  2. class GoogleWeatherAPI {  
  3.     private $city_code = '';  
  4.     private $city = '';  
  5.     private $domain = 'www.google.com';  
  6.     private $prefix_images = '';  
  7.     private $current_conditions = array();  
  8.     private $forecast_conditions = array();  
  9.     private $is_found = true;  
  10.   
  11.     /**  
  12.     * Class constructor  
  13.     * @param $city_code is the label of the city  
  14.     * @param $lang the lang of the return weather labels  
  15.     * @return ...  
  16.     */  
  17.        
  18.     function __construct ($city_code,$lang='fr') {  
  19.         $this->city_code = $city_code;  
  20.         $this->prefix_images = 'http://'.$this->domain;  
  21.         $this->url = 'http://'.$this->domain.'/ig/api?weather='.urlencode($this->city_code).'&hl='.$lang;  
  22.           
  23.         $content = utf8_encode(file_get_contents($this->url));  
  24.           
  25.         $xml = simplexml_load_string($content);  
  26.           
  27.         if(!isset($xml->weather->problem_cause)) {  
  28.               
  29.             $xml = simplexml_load_string($content);  
  30.   
  31.             $this->city = (string)$xml->weather->forecast_information->city->attributes()->data;  
  32.   
  33.             $this->current_conditions['condition'] = (string)$xml->weather->current_conditions->condition->attributes()->data;  
  34.             $this->current_conditions['temp_f'] = (string)$xml->weather->current_conditions->temp_f->attributes()->data;  
  35.             $this->current_conditions['temp_c'] = (string)$xml->weather->current_conditions->temp_c->attributes()->data;  
  36.             $this->current_conditions['humidity'] = (string)$xml->weather->current_conditions->humidity->attributes()->data;  
  37.             $this->current_conditions['icon'] = $this->prefix_images.(string)$xml->weather->current_conditions->icon->attributes()->data;  
  38.             $this->current_conditions['wind_condition'] = (string)$xml->weather->current_conditions->wind_condition->attributes()->data;  
  39.               
  40.             foreach($xml->weather->forecast_conditions as $this->forecast_conditions_value) {  
  41.                 $this->forecast_conditions_temp = array();  
  42.                 $this->forecast_conditions_temp['day_of_week'] = (string)$this->forecast_conditions_value->day_of_week->attributes()->data;  
  43.                 $this->forecast_conditions_temp['low'] = (string)$this->forecast_conditions_value->low->attributes()->data;  
  44.                 $this->forecast_conditions_temp['high'] = (string)$this->forecast_conditions_value->high->attributes()->data;  
  45.                 $this->forecast_conditions_temp['icon'] = $this->prefix_images.(string)$this->forecast_conditions_value->icon->attributes()->data;  
  46.                 $this->forecast_conditions_temp['condition'] = (string)$this->forecast_conditions_value->condition->attributes()->data;  
  47.                 $this->forecast_conditions []= $this->forecast_conditions_temp;  
  48.             }  
  49.         } else {  
  50.             $this->is_found = false;  
  51.         }  
  52.     }  
  53.     function getCity() {  
  54.         return $this->city;  
  55.     }  
  56.     function getCurrent() {  
  57.         return $this->current_conditions;  
  58.     }  
  59.     function getForecast() {  
  60.         return $this->forecast_conditions;  
  61.     }  
  62.     function isFound() {  
  63.         return $this->is_found;  
  64.     }  
  65.       
  66. }  
  67. $gweather = new GoogleWeatherAPI('valencia','es');   
  68. if($gweather->isFound()) {  
  69.     echo '<pre>'; print_r($gweather->getCity()); echo '</pre>';  
  70.     echo '<pre>'; print_r($gweather->getCurrent()); echo '</pre>';  
  71.     echo '<pre>'; print_r($gweather->getForecast()); echo '</pre>';  
  72. }  
  73. ?>  
Ver ejemplo en funcionamiento » »
Google Weather API » »

No hay comentarios: