Foros del Web » Programando para Internet » PHP »

[Aporte] El tiempo en tu web con google weather

Estas en el tema de [Aporte] El tiempo en tu web con google weather en el foro de PHP en Foros del Web. 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 ...
  #1 (permalink)  
Antiguo 06/05/2010, 15:41
Avatar de mark_ant0n  
Fecha de Ingreso: enero-2009
Ubicación: Comitan, Chiapas mx
Mensajes: 388
Antigüedad: 15 años, 3 meses
Puntos: 6
[Aporte] El tiempo en tu web con google weather

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
  1. <html>
  2. <head>
  3. <title>Api de google</title>
  4. <style type="text/css">
  5. .pronostico{width: 170px;height: 45px;background: #000 no-repeat;background-position: right;float:left;}
  6. .temperatura{width: 345px; height: 45px;background: #91ab0b;border-bottom: solid 3px #313131;}
  7. .imgtem{width:30px;height:45px;float:left;}
  8. .imgtem img{margin: 7px auto;border: solid 2px gray;}
  9. .protem{width:140px;height:45px;float:left; font-size:11px;}
  10. .cguinda{text-align:left;color:#eb0686;float:left;margin-left: 1px;}
  11. .cverde{text-align:left;color:#91ab0b;float:left;margin-left: 1px;}
  12. .cceleste{text-align:left;color:#009ee0;float:left;margin-left: 1px;}
  13. .cblanco{text-align:left;color: white;float:left;margin-left: 2px;}
  14. </style>
  15. </head>
  16. <body>
  17. <?php
  18. class GoogleWeatherAPI {
  19.     private $city_code = '';
  20.     private $city = '';
  21.     private $domain = 'www.google.com';
  22.     private $prefix_images = '';
  23.     private $current_conditions = array();
  24.     private $forecast_conditions = array();
  25.     private $is_found = true;
  26.  
  27.     /**
  28.     * Class constructor
  29.     * @param $city_code is the label of the city
  30.     * @param $lang the lang of the return weather labels
  31.     * @return ...
  32.     */
  33.      
  34.     function __construct ($city_code,$lang='fr') {
  35.         $this->city_code = $city_code;
  36.         $this->prefix_images = 'http://'.$this->domain;
  37.         $this->url = 'http://'.$this->domain.'/ig/api?weather='.urlencode($this->city_code).'&hl='.$lang;
  38.        
  39.         $content = utf8_encode(file_get_contents($this->url));
  40.        
  41.         $xml = simplexml_load_string($content);
  42.        
  43.         if(!isset($xml->weather->problem_cause)) {
  44.            
  45.             $xml = simplexml_load_string($content);
  46.  
  47.             $this->city = (string)$xml->weather->forecast_information->city->attributes()->data;
  48.  
  49.             $this->current_conditions['condition'] = (string)$xml->weather->current_conditions->condition->attributes()->data;
  50.             $this->current_conditions['temp_f'] = (string)$xml->weather->current_conditions->temp_f->attributes()->data;
  51.             $this->current_conditions['temp_c'] = (string)$xml->weather->current_conditions->temp_c->attributes()->data;
  52.             $this->current_conditions['humidity'] = (string)$xml->weather->current_conditions->humidity->attributes()->data;
  53.             $this->current_conditions['icon'] = $this->prefix_images.(string)$xml->weather->current_conditions->icon->attributes()->data;
  54.             $this->current_conditions['wind_condition'] = (string)$xml->weather->current_conditions->wind_condition->attributes()->data;
  55.            
  56.             foreach($xml->weather->forecast_conditions as $this->forecast_conditions_value) {
  57.                 $this->forecast_conditions_temp = array();
  58.                 $this->forecast_conditions_temp['day_of_week'] = (string)$this->forecast_conditions_value->day_of_week->attributes()->data;
  59.                 $this->forecast_conditions_temp['low'] = (string)$this->forecast_conditions_value->low->attributes()->data;
  60.                 $this->forecast_conditions_temp['high'] = (string)$this->forecast_conditions_value->high->attributes()->data;
  61.                 $this->forecast_conditions_temp['icon'] = $this->prefix_images.(string)$this->forecast_conditions_value->icon->attributes()->data;
  62.                 $this->forecast_conditions_temp['condition'] = (string)$this->forecast_conditions_value->condition->attributes()->data;
  63.                 $this->forecast_conditions []= $this->forecast_conditions_temp;
  64.             }
  65.         } else {
  66.             $this->is_found = false;
  67.         }
  68.     }
  69.     function getCity() {
  70.         return $this->city;
  71.     }
  72.     function getCurrent() {
  73.         return $this->current_conditions;
  74.     }
  75.     function getForecast() {
  76.         return $this->forecast_conditions;
  77.     }
  78.     function isFound() {
  79.         return $this->is_found;
  80.     }
  81.    
  82. }
  83. $gweather = new GoogleWeatherAPI('comitan de dominguez,chiapas','es');
  84. if($gweather->isFound()) {
  85.      $contenido=$gweather->getCurrent();    
  86.      $pronostico=$gweather->getForecast();
  87. }
  88. ?>
  89. <div class="temperatura">
  90. <div class="pronostico"><div class="imgtem"><img src='<?php echo $contenido['icon'];?>' width='24'/></div><div class="protem">
  91. <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 />
  92. <div class="cblanco"></div><div class="cverde"><?php echo $contenido['condition']; ?></div><br />
  93. <div class="cblanco"><?php echo $pronostico['0']['condition']; ?></div><br />
  94. </div></div>
  95.  
  96. <div class="pronostico"><div class="imgtem"><img src='<?php echo $pronostico['1']['icon'];?>' width='24'/></div><div class="protem">
  97. <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 />
  98. <div class="cverde"><?php echo $pronostico['1']['condition']; ?></div><br />
  99. </div></div>
  100.  
  101.  
  102. </div>
  103.  
  104. </body>
  105. </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
  1. $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 original
  1. if($gweather->isFound()) {
  2.  $contenido=$gweather->getCurrent();    
  3.     $pronostico=$gweather->getForecast();
  4. }
__________________
"Diseño de software a la medida"

http://www.sadhoc.com

Etiquetas: google, weather, aportes
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 07:46.