(redirected from Main.GoogleWeatherApi)
The iGoogle weather api is an undocument service profided by Google to serve it's iGoogle home page weather widget. It is by far the easiest weather web service to use, as everything is broken out for you in XML.
The call couldn't be easier, really:
http://www.google.com/ig/api?weather=City%20Name
You can specify a state abbreviation as well, for example, St. Paul, Minnesota:
http://www.google.com/ig/api?weather=St.%20Paul%2C%20MN
You can also specify a zip code:
http://www.google.com/ig/api?weather=55112
Here's an example of the XML output (tidied up) that you get for the city of "Minneapolis":
<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="Minneapolis, MN"/>
<postal_code data="Minneapolis"/>
<latitude_e6 data=""/>
<longitude_e6 data=""/>
<forecast_date data="2011-11-19"/>
<current_date_time data="2011-11-20 01:53:00 +0000"/>
<unit_system data="US"/>
</forecast_information>
<current_conditions>
<condition data="Light snow"/>
<temp_f data="27"/>
<temp_c data="-3"/>
<humidity data="Humidity: 81%"/>
<icon data="/ig/images/weather/flurries.gif"/>
<wind_condition data="Wind: NW at 10 mph"/>
</current_conditions>
<forecast_conditions>
<day_of_week data="Sat"/>
<low data="16"/>
<high data="32"/>
<icon data="/ig/images/weather/snow.gif"/>
<condition data="Snow"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="Sun"/>
<low data="25"/>
<high data="27"/>
<icon data="/ig/images/weather/mostly_sunny.gif"/>
<condition data="Mostly Sunny"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="Mon"/>
<low data="20"/>
<high data="36"/>
<icon data="/ig/images/weather/mostly_sunny.gif"/>
<condition data="Mostly Sunny"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="Tue"/>
<low data="25"/>
<high data="38"/>
<icon data="/ig/images/weather/mostly_sunny.gif"/>
<condition data="Mostly Sunny"/>
</forecast_conditions></weather>
</xml_api_reply>
A real simple script to get the current conditions:
- use strict;
- use XML::Simple;
- use URI::Escape;
- use constant DEFAULT_CITY => 'Minneapolis';
- use Getopt::Mixed::Help(
- 'c>city:s city' => "City to look up weather for"
- );
- my $city = $opt_city;
- my $url = "http://www.google.com/ig/api?weather=" . uri_escape($city);
- my $curlcmd = "curl -s $url";
- my $weather_xml = `$curlcmd`;
- my $w = XMLin($weather_xml);
- printf("Current conditions for %s on %s:
- Temp: %s F (%s C)
- Wind: %s
- Humidity: %s
- Sky: %s
- ",
- $w->{weather}->{forecast_information}->{city}->{data},
- $w->{weather}->{forecast_information}->{forecast_date}->{data},
- $w->{weather}->{current_conditions}->{temp_f}->{data},
- $w->{weather}->{current_conditions}->{temp_c}->{data},
- $w->{weather}->{current_conditions}->{wind_condition}->{data},
- $w->{weather}->{current_conditions}->{humidity}->{data},
- $w->{weather}->{current_conditions}->{condition}->{data}
- );
Prints on stdout:
Current conditions for Minneapolis, MN on 2011-11-19: Temp: 27 F (-3 C) Wind: Wind: NW at 10 mph Humidity: Humidity: 81% Sky: Light snow
| Tags: | Categories: Articles,HowTos |
Recent Changes |
Printable View |
Page History |
Edit Page
Page last modified on April 16, 2012, at 12:58 PM by tamara