Mengambil Data Cuaca dengan Google API

weather pictureJika anda ingin menunjukan cuaca, anda dapat menggunakan semacam widget seperti weatherbug. Mudah, tetapi mungkin anda membutuhkan sesuatu yang lebih terintegrasi dengan website anda atau harus mengolah data tersebut terlebih dahulu. Mari kita mencoba menggunakan Google Weather API.

http://www.google.com/ig/api?weather=[nama kota]

Contoh :

http://www.google.com/ig/api?weather=jakarta

Kode tersebut akan memberikan response berupa data XML, dan kita bisa parse data tersebut dengan mudah di PHP. Lihatlah kode berikut :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?
$xml = simplexml_load_file('http://www.google.com/ig/api?weather=jakarta');
$information = $xml->xpath("/xml_api_reply/weather/forecast_information");
$current = $xml->xpath("/xml_api_reply/weather/current_conditions");
$forecast_list = $xml->xpath("/xml_api_reply/weather/forecast_conditions");
?>
<html>
    <head>
        <title>Google Weather API</title>
    </head>
    <body>
        <h1><?= print $information[0]->city['data']; ?></h1>
        <h2>Today's weather</h2>
        <div class="weather">		
            <img src="<?= 'http://www.google.com' . $current[0]->icon['data']?>" alt="weather"?>
            <span class="condition">
            <?= $current[0]->temp_f['data'] ?>&deg; F,
            <?= $current[0]->condition['data'] ?>
            </span>
        </div>
        <h2>Forecast</h2>
        <? foreach ($forecast_list as $forecast) : ?>
        <div class="weather">
            <img src="<?= 'http://www.google.com' . $forecast->icon['data']?>" alt="weather"?>
            <div><?= $forecast->day_of_week['data']; ?></div>
            <span class="condition">
	            <?= $forecast->low['data'] ?>&deg; F - <?= $forecast->high['data'] ?>&deg; F,
	            <?= $forecast->condition['data'] ?>
            </span>
        </div>	
        <? endforeach ?>
    </body>
</html>

Pada kenyataannya, mungkin anda harus mempertimbangkan untuk menaruh hasil tersebut dalam cache, jadi anda tidak perlu memanggil google API secara terus menerus, juga dengan pertimbangan bahwa cuacu berubah dalam hitungan hari. Selamat mencoba.

Related posts:

  1. Gmail + Social = Google Buzz
  2. Apa Itu Google App Engine?
  3. Membuat Microblog Sederhana Dengan CodeIgniter
  4. Pagination Tutorial Dengan CodeIgniter
  5. Shopping Cart Dengan CodeIgniter

About Gilang Chandrasa

Gilang Chandrasa adalah seorang web developer yang sangat menyukai dunia open source. Memasak adalah salah satu hobinya diluar dunia IT. Pendiri Yumbaa dan Komunitaz ini juga adalah seorang blogger, menulis di berbagai situs termasuk komunitasweb. Mempunyai mimpi untuk memajukan dunia web Indonesia.
This entry was posted in Panduan and tagged . Bookmark the permalink.

One Response to Mengambil Data Cuaca dengan Google API

  1. louise says:

    bisa bantu saya ga ? kalau application weather untu android gmn c api nya?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Additional comments powered by BackType