Predpoveď počasia (Google)

Nasledujúci článok slúži na získanie údajov o počasí pomocou Google API pre zadané mesto na niekoľko dní. Informácie sa ukladajú do poľa, ktorého štruktúra je popísaná v príkladoch použitia. V prípade, že zadáte mesto, ktoré google nepozná, vracia sa prázdne pole. Reálne by bolo račej vhodné použiť XML, je to rýchlejšie a taktiež použiť vlastné obrázky a neťahať ich z google.

Samotný script
<?php
 
class GoogleWeather {
   var $lang = "sk";
   var $country = "Slovakia";
   var $charset = "utf-8";
 
   function getWeatherObj($city) {
      $city = urlencode($city);
      $data = @file_get_contents("http://www.google.com/ig/api?weather=".$city.",".$this->country."&hl=".$this->lang);
      if(!$data) {
         return false;
      }
      $data = iconv("ISO-8859-2",$this->charset,$data);
      return new SimpleXMLElement($data);
   }
 
   function getWeatherInfo($city) {
      $weather = array();
      $obj = $this->getWeatherObj($city);
 
      if(is_object($obj)) {
         $data = $obj->xpath("/xml_api_reply/weather/current_conditions");
         if(!empty($data)) {
            $weather[] = array(
               "condition"      => $this->getAttr($data[0]->xpath("condition")),
               "temp_f"         => $this->getAttr($data[0]->xpath("temp_f")),
               "temp_c"         => $this->getAttr($data[0]->xpath("temp_c")),
               "humidity"      => $this->getAttr($data[0]->xpath("humidity")),
               "icon"         => $this->getAttr($data[0]->xpath("icon")),
               "wind_condition"   => $this->getAttr($data[0]->xpath("wind_condition"))
            );
         }
 
         $data = $obj->xpath("/xml_api_reply/weather/forecast_conditions");
         if(!empty($data)) {
            foreach($data as $value) {
               $weather[] = array(
                  "day_of_week"   => $this->getAttr($value->xpath("day_of_week")),
                  "low"      => $this->getAttr($value->xpath("low")),
                  "high"      => $this->getAttr($value->xpath("high")),
                  "icon"      => $this->getAttr($value->xpath("icon")),
                  "condition"   => $this->getAttr($value->xpath("condition"))
               );
            }
         }
      }
 
      return $weather;
   }
 
   function getAttr($obj) {
      if(is_object($obj[0])) {
         $attr = $obj[0]->attributes();
      }
      return (isset($attr['data'])) ? (string)$attr['data'] : '';
   }
}
?>
Príklad použitia
$gw = new GoogleWeather();
$weather = $gw->getWeatherInfo("Bratislava");   //pocasie pre Bratislavu
print_r($weather);
//$weather = $gw->getWeatherInfo("Kosice");   //pocasie pre Kosice
//print_r($weather);
Výsledok
Array
(
    [0] => Array
        (
            [condition] => Prevažne oblačno
            [temp_f] => 45
            [temp_c] => 7
            [humidity] => Vlhkosť: 61%
            [icon] => /images/weather/mostly_cloudy.gif
            [wind_condition] => Vietor: J s rýchlosťou 0 km/h
        )
 
    [1] => Array
        (
            [day_of_week] => Ut
            [low] => -2
            [high] => 8
            [icon] => /images/weather/chance_of_rain.gif
            [condition] => Občasný dážď
        )
 
    [2] => Array
        (
            [day_of_week] => St
            [low] => 2
            [high] => 5
            [icon] => /images/weather/chance_of_snow.gif
            [condition] => Občasné sneženie
        )
 
    [3] => Array
        (
            [day_of_week] => Št
            [low] => 1
            [high] => 8
            [icon] => /images/weather/chance_of_rain.gif
            [condition] => Občasný dážď
        )
 
    [4] => Array
        (
            [day_of_week] => Pi
            [low] => 0
            [high] => 10
            [icon] => /images/weather/chance_of_rain.gif
            [condition] => Občasný dážď
        )
 
)
Vysvetlivky
  • [0] - pole s aktuálnym počasím
  • [1] - pole s počasím na dnes
  • [2] - pole s počasím na zajtra
  • [3] - pole s počasím na pozajtra
  • [4] - pole s počasím na popozajtra
  • [condition] - slovný popis počasia
  • [temp_f] - teplota vo F
  • [temp_c] - teplota v C
  • [humidity] - vlhkosť vzduchu
  • [icon] - obrázok (slnko, dážď, …)
  • [wind_condition] - veterné podmienky
  • [day_of_week] - skratka názvu dňa
  • [low] - najnižšia teplota
  • [high] - najvyššia teplota
Praktický príklad
$gw = new GoogleWeather();
 
$arrCity = array("Bratislava","Kosice","Poprad","Zilina","Nitra","Trnava","Trencin");
$content = '<table>';
foreach($arrCity as $city) {
   $weather = $gw->getWeatherInfo($city);
 
   if(!empty($weather)) {
      $content .= '<tr>';
      $content .= '<td rowspan="2"><img src="'.$weather[1]['icon'].'"></td>';
      $content .= '<td width="120"><strong>'.$city.'</strong></td>';
      $content .= '<td>'.$weather[1]['high'].'&deg;C / '.$weather[1]['low'].'&deg;C</td>';
      $content .= '</tr><tr>';
      $content .= '<td colspan="2">'.$weather[1]['condition'].'</td>';
      $content .= '</tr>';
   }
}
$content .= '</table>';
echo $content;
Odkazy
 
predpoved_pocasi_google.txt · Poslední úprava: 2011/09/24 11:36 autor: xsouku04