The other day, I was looking for a free weather forecast api for a project I’m working on. I managed to find a few, but the one that seemed to be the fastest and easiest to implement was ”The secret Google Weather API” (that’s the way it was called in few posts I’ve read).
Anyhow, the “Free Computer Advice” blog, had a nice post that explained how to use this weather API with PHP, but since the site went down on Dec 2011 it’s not accessible anymore.
Luckily, I managed to snag the post’s content from Google Cache before it was removed entirely, so here it is for your pleasure.
Sunday, August 2, 2009
Google Weather API
To start lets pull up the URL in your browser.
You’ll see XML data of the weather in New York! Easy enough, let’s take a look at the output.
- <xml_api_reply version=”1″>
- <weather module_id=”0″ tab_id=”0″ mobile_row=”0″ mobile_zipped=”1″ row=”0″ section=”0″ >
- <forecast_information>
- <city data=”New York, NY”/>
- <postal_code data=”new york,ny”/>
- <latitude_e6 data=” “/>
- <longitude_e6 data=” “/>
- <forecast_date data=”2009-08-02″/>
- <current_date_time data=”2009-08-02 12:19:00 +0000″/>
- <unit_system data=”US”/>
- </forecast_information>
- <current_conditions>
- <condition data=”Light rain”/>
- <temp_f data=”72″/>
- <temp_c data=”22″/>
- <humidity data=”Humidity: 94%”/>
- <icon data=”/ig/images/weather/mist.gif”/>
- <wind_condition data=”Wind: N at 6 mph”/>
- </current_conditions>
- </weather>
- </xml_api_reply>
Wow check out how easy that was. Now all we did was submit the location and hit enter. Now we have in XML weather data for that area. No advertisements and since the data is in XML we can make it look however we want. Note: I shortened the output to only one day. So what’s next? Well in my case I used a PHP script to output this data. Let’s take a look at that source code.
- <?php
- function getWeather() {
- $requestAddress = “http://www.google.com/ig/api?weather=21619&hl=en”;
- // Downloads weather data based on location – I used my zip code.
- $xml_str = file_get_contents($requestAddress,0);
- // Parses XML
- $xml = new SimplexmlElement($xml_str);
- // Loops XML
- $count = 0;
- echo ‘<div id=”weather”>’;
- foreach($xml->weather as $item) {
- foreach($item->forecast_conditions as $new) {
- echo ‘<div class=”weatherIcon”>’;
- echo ‘<img src=”http://www.google.com/’ . $new->icon['data'] . ‘”/><br/>’;
- echo $new->day_of_week['data'];
- echo ‘</div>’;
- }
- }
- echo ‘</div>’;
- }
- getWeather();
- ?>
I hope this tutorial was helpful! The Weather API is great on so many levels, fast, easy and customizable.

Nice post… I’ve also used a weather API for one of my clients as part of a .net development project. This is the site that I’ve sued as a reference: http://awapi.codeplex.com/
You can ask the google weather for multiple places like below:
$xml = simplexml_load_file(‘http://www.google.com/ig/api?weather=Amsterdam&weather=Cambodja’);
//$information = $xml->xpath(“/xml_api_reply/weather/forecast_information”);
$current = $xml->xpath(“/xml_api_reply/weather/current_conditions”);
echo ‘
icon['data'].’” alt=”weather”>
‘.$current[0]->temp_c['data'].’° C
‘;
$city["Name"] = “Amsterdam”;
$city["GMT"] = 1.0;
$city["actualDST"] = 0.0; //Because it’s summer time
$gmt_diff = $city["GMT"]+$city["actualDST"]; //your functions for getting the hour difference betweer the city and the GMT
$city_time = time()+($gmt_diff*3600); //sum the timestamps
//echo ”.gmdate(“”,$city_time); //echo the formatted date
echo ‘ ’.gmdate(“H:i”,$city_time).’ ’;
echo ‘
icon['data'].’” alt=”weather2″>
‘.$current[1]->temp_c['data'].’° C
‘;
$city["Name"] = “Phnom Penh”;
$city["GMT"] = 7.0;
$city["actualDST"] = 0.0; //Because it’s summer time
$gmt_diff = $city["GMT"]+$city["actualDST"]; //your functions for getting the hour difference betweer the city and the GMT
$city_time = time()+($gmt_diff*3600); //sum the timestamps
//echo ”.gmdate(“”,$city_time); //echo the formatted date
echo ‘ ’.gmdate(“H:i”,$city_time).’ ’;
echo ‘