
If you need to show weather in your website, you can use weather widget such as weatherbug. It’s nice and simple, but maybe you need something more integrated with your website. So, take a look at Google Weather API.
http://www.google.com/ig/api?weather=[city name]
Example :
http://www.google.com/ig/api?weather=jakarta
It will give you xml data, and you can parse it easy in PHP. Take a look at sample code.
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'] ?>° 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'] ?>° F - <?= $forecast->high['data'] ?>° F, <?= $forecast->condition['data'] ?> </span> </div> <? endforeach ?> </body> </html> |
In real world, you need to take consideration to cache the result, you don’t need to call the google API each time as the weather change daily. That’s it. Happy coding everyone.
You can see the weather at Jakarta.
Related posts:
September 8th, 2009 | Comments(14)

[...] Fonte:komunitasweb.com [...]
Pingback by Mostrare le previsioni del tempo con PHP e Google Weather API - Caputo’s blog - Informatica, tecnologia, programmazione, fai da te, papercraft e papertoy — September 8, 2009 @ 9:45 pm
[...] the Komunitasweb.com blog there’s a recent post walking you through the steps to add the Google weather content to your site, complete with icons. [...]
Pingback by Komunitasweb.com: Showing the weather with PHP and Google Weather API | Webs Developer — September 11, 2009 @ 10:02 pm
Thanks! This will be very useful, I was wandering wich way could do this in some of my websites.
Regards.
Comment by Mauricio Rivera — September 11, 2009 @ 11:53 pm
[...] the Komunitasweb.com blog there’s a recent post walking you through the steps to add the Google weather content to your site, complete with icons. [...]…
Comment by Akinyooye — September 12, 2009 @ 11:12 am
[...] em: Showing the weather with PHP and Google Weather API GHTime Code(s): b2b30 45e6b a7525 (Sem votos ainda) Loading … publicidade [...]
Pingback by Mostrando a Previsão do Tempo com PHP | Thiago Belem / Blog — September 13, 2009 @ 11:53 am
[...] I came across a post recently which outlined how easy it is to output weather with php and Google Weather API [...]
Pingback by Make a google weather plugin in Zend Framework — September 15, 2009 @ 12:27 pm
Awesome post. One problem with using this method is that most shared servers won’t have “allow_url_fopen = On” set in php.ini (for security reasons). So I did some preliminary steps to get the data in using CURL, which many servers do have. I’ll try and paste it here… though not sure if the comment will display correctly:
$ch = curl_init(“http://www.google.com/ig/api?weather=04843″);
$fp = fopen(“camden_weather.xml”, “w”);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
$xml = simplexml_load_file(‘camden_weather.xml’);
… no changes to rest of PHP or HTML
Nice work! Thanks!
Comment by Sparkybarkalot — September 25, 2009 @ 9:42 pm
Forgot to mention that another nice thing about using CURL is that you can then cache that XML file and have your code check first to see if there’s a cached version. My code doesn’t include that, but it’s not a bad idea to have.
Comment by Sparkybarkalot — September 25, 2009 @ 9:47 pm
[...] The rest is here: Showing the weather with PHP and Google Weather API | KomunitasWeb [...]
Pingback by Showing the weather with PHP and Google Weather API | KomunitasWeb | My Web Development Bookmarks — September 28, 2009 @ 1:10 am
[...] Showing the weather with PHP and Google Weather API [...]
Pingback by Lenguajes X » Mostrar el tiempo con PHP y Google Weather API — October 7, 2009 @ 5:26 pm
nice information, i will try to do something in my sites
Comment by developtus — October 7, 2009 @ 7:30 pm
Muy buen artículo, sólo que en el demo que publicaste, genera un error cuando se quiere buscar una ciudad como por ej. “Buenos Aires”
Además sería interesante poder agregar un filtro adicional cosa de poder diferenciar algunas ciudades con el mismo nombre, por ej. Córdoba , que existe en Argentina y España
Saludos
This comment was originally posted on unijimpe
Comment by Christian — October 20, 2009 @ 5:53 pm
Iba a decir exactamente lo mismo que Christian. Yo por ejemplo vivo en Córdoba, Argentina… y no puedo ver los datos del tiempo de mi ciudad.
Me encanta la web, y estoy sacando mucha info que hay aca, y estoy rediseñando mi web ahora gracias a toda la info que pones, y a mucha otra que complementa que encontre en google.
Gracias, te felicito.
This comment was originally posted on unijimpe
Comment by Tincho — October 20, 2009 @ 7:31 pm
Gracias por el post, me será de ayuda en mis proyectos.
Lo del espacio podrias solucionarlo reemplazando en el php que recibe el campo de la busqueda los espacios por el signo de +, para que el file_get_contents no contenga espacios y funcione sin problema!
Saludos.
This comment was originally posted on unijimpe
Comment by Carlos G. — October 20, 2009 @ 8:47 pm