.:: Script Prakiraan Cuaca dengan Google Weather API ::.

Dikirim oleh : Po3nX, pada 20-02-2010, kategori : Tips & Trick
"

Pernah liat info prakiraan cuaca yang di iGoogle? Pengen bisa pasang widget cuaca yang seperti itu? Berikut saya coba sharing script untuk menampilkan prakiraan cuaca yang mengambil data dari google weather api.."

Langsung saja berikut adalah source phpnya, silahkan modifikasi tampilannya sesuai selera anda

1. Buat class yang berfungsi untuk mengambil data2 dari google weather api sebagai berikut:

class_weather.php

  1.  
  2. <?php
  3.  
  4. class weather
  5. {
  6.         public static $response;
  7.         public static $location;
  8.         public static $current;
  9.         public static $nextdays;
  10.         public static $error = false;
  11.        
  12.         public function weather()
  13.         {
  14.                 $this->location = 'Jakarta';
  15.         }
  16.        
  17.         public function get()
  18.         {
  19.                 if (empty($this->location)) {
  20.                         $this->error = true;
  21.                         return false;
  22.                 }
  23.                 $requestAddress = "http://www.google.com/ig/api?weather=".trim(urlencode($this->location))."&hl=en";
  24.                 $xml_str = file_get_contents($requestAddress,0);
  25.                 $xml = new SimplexmlElement($xml_str);
  26.                 if (!$xml->weather->problem_cause) {
  27.                         $this->response = $xml->weather;
  28.                         $this->parse();
  29.                 }else{
  30.                         $this->error = true;
  31.                 }
  32.         }
  33.        
  34.         public function parse()
  35.         {
  36.                 foreach($this->response as $item) {
  37.                         $this->current = $item->current_conditions;
  38.                         foreach($item->forecast_conditions as $new) {
  39.                                 $this->nextdays[] = $new;              
  40.                         }      
  41.                 }
  42.         }
  43.        
  44.         public function display()
  45.         {
  46.                 echo "<table border=0 cellspacing=0 cellpadding=3><tr>";
  47.                 foreach($this->nextdays as $new) {                     
  48.                         echo '<td align=center>';
  49.                                 echo '<b>'.$new->day_of_week['data'].'</b><br>';
  50.                                 echo '<img src="http://www.google.com/' .$new->icon['data'] . '"/>';
  51.                                 echo '<small>'.$new->condition['data'].'</small>';
  52.                                 echo ''.$this->convert($new->low['data']).' &#8451;';
  53.                                 echo '|'.$this->convert($new->high['data']).' &#8451;';
  54.                         echo '</td>';                  
  55.                 }      
  56.                 echo "</tr></table>";
  57.         }
  58.        
  59.         public function convert($value, $unit = "C"){
  60.                 switch($unit){
  61.                         case "C":
  62.                                 return number_format(($value - 32)/1.8);
  63.                         break;
  64.                         case "F":
  65.                                 return round($value * 1.8 + 32);
  66.                         break;
  67.                         default:
  68.                                 return $value;
  69.                                 break;
  70.                 };
  71.         }      
  72. }
  73. ?>
  74.  



2. Kemudian buat script untuk menampilkan class tersebut

cuaca.php
  1.  
  2. <?php
  3. require_once('class_weather.php');
  4.  
  5. $weather = new weather();
  6. if (!empty($_GET['loc'])) {
  7.         $weather->location = $_GET['loc'];
  8. }
  9. $weather->get();
  10. if($weather->error){
  11.         die('We couldn't find your location.');
  12. }else{
  13.         echo '
  14.         <table border=0 cellspacing=0 cellpadding=3><tr>
  15.                 <td colspan=3>Prakiraan Cuaca Kota :'.ucwords($weather->location).'</td></tr>
  16.                 <tr><td><img src="http://www.google.com/' .$weather->current->icon['data'] . '" /></td>
  17.                 <td><h2>'.$weather->current->temp_c['data'].' &#8451;</h2></td>
  18.                 <td>'.$weather->current->condition['data'].'<br/>
  19.                 '.$weather->current->humidity['data'].'<br/>
  20.                 '.$weather->current->wind_condition['data'].'</td>
  21.         </tr></table>
  22.         ';
  23.         $weather->display();
  24. }
  25. ?>
  26.  


anda bisa mencobanya di localhost atau mengupload ke server anda lalu membukanya pada browser anda, jika tidak ada kesalahan anda akan mendapatkan hasil kurang lebih seperti ini



Untuk mencoba script yang saya buat anda bisa membuka di halaman berikut:

http://po3nx.web.id/index.php?section=cuaca

untuk melihat prakiraan cuaca di daerah lain anda dapat menambahkan variable loc pada url misalkan begini

http://po3nx.web.id/index.php?section=cuaca&loc=yogyakarta <== untuk melihat prakiraan cuaca daerah Yogyakarta

catatan: belum semua kota di Indonesia atau didunia ada dalam database, tetapi sudah hampir semua kota yang merupakan ibukota propinsi di Indonesia sudah bisa dilihat di database.

Selamat mencoba semoga bermanfaat

Terimakasih

sumber class: PHPClasses

Komentar Pembaca : 5 komentar

pada : 20-02-2010, [Paman Gugel ] menulis :
Gravatar

Nice post :D

pada : 27-02-2010, [Tamu ] menulis :
Gravatar

Ganaz ew si om, paman gugel aja bisa komen disini, wakakakakak., kacaw

pada : 02-03-2010, [Po3nX ] menulis :
Gravatar

Wologh... Paman Gugel dari hongkong.. weekkekek.. peace man (Paman)..

pada : 27-04-2010, [Juliana ] menulis :
Gravatar

Mas, kalo script ini dipake di web joomla bisa ga ya? Apa caranya juga sama? Thanks jawabannya.. :-)

pada : 27-04-2010, [Po3nX ] menulis :
Gravatar

Yup, tentu bisa, cuman caranya agak berbeda, kita perlu membuat modul baru, dan menginstallnya d joomla, tentang bagaimana cara membuat modul di joomla bisa dilihat di url :
http://docs.joomla.org/How_to_create_a_module

Ikut Mengomentari

Nama *:
Web:
Email:
Komentar *:
Verivication code :
Write code here *:


Note : Data dengan tanda * harus diisi
allowed :