
A reliable, easy-to-search, international weather feed service is hard to come by. There are lots out there, but they either require a complicated sign-up process, are limited to a single country, or make searching for location weather a multi-step process.
As it turns out, Google has a key-free "secret," undocumented weather API that is commonly used for iGoogle. Surprisingly, there is no sign-up process, it's international, and there's only one feed file to deal with. Getting the feed for a location is as simple as calling: http://www.google.com/ig/api?weather=Mountain+View.
Here's a quick class I whipped up that you can use. It caches and parses that XML file into a friendly array. Modifications may be necessary for it to work properly with your server install. You use it at your own risk. You can change the cache time to whatever you like, but I wouldn't hit Google's servers too hard.
Rain - 18°F - Wind: N at 2 mph - Humidty: 93%
Forecast date: 2/9/2012 07:22 UTC
Thu - Partly Sunny: 19°F/34°F
Fri - Mostly Sunny: 19°F/34°F
Sat - Mostly Sunny: 16°F/32°F
Sun - Mostly Sunny: 23°F/34°F
In order to get weather for a location, we simply pass our search string, like "Paris" below and let Google handle the magic. The result is a data array that we can print_r to see all the info.
require('google.weather.class.php'); $weather = new GoogleWeather("Paris", "m"); if($data = $weather->get_conditions()) { echo '<pre>'.print_r($data, TRUE).'</pre>'; } else { echo 'No data available for this location'; }
Array ( [forecast_information] => Array ( [city] => Paris, Île-de-France [postal_code] => paris [latitude_e6] => [longitude_e6] => [forecast_date] => 2012-02-09 [current_date_time] => 2012-02-09 07:22:40 +0000 [unit_system] => US [timestamp] => 1328772160 ) [current_conditions] => Array ( [condition] => Rain [humidity] => 93% [icon] => icons/m/rain.gif [temp] => Array ( [f] => 18 [c] => -8 ) [wind] => Array ( [mph] => N at 2 mph [kph] => N at 3 kph ) ) [forecast_conditions] => Array ( [0] => Array ( [day_of_week] => Thu [icon] => icons/m/mostly_sunny.gif [condition] => Partly Sunny [high] => Array ( [f] => 34 [c] => 1 ) [low] => Array ( [f] => 19 [c] => -7 ) ) [1] => Array ( [day_of_week] => Fri [icon] => icons/m/mostly_sunny.gif [condition] => Mostly Sunny [high] => Array ( [f] => 34 [c] => 1 ) [low] => Array ( [f] => 19 [c] => -7 ) ) [2] => Array ( [day_of_week] => Sat [icon] => icons/m/mostly_sunny.gif [condition] => Mostly Sunny [high] => Array ( [f] => 32 [c] => 0 ) [low] => Array ( [f] => 16 [c] => -9 ) ) [3] => Array ( [day_of_week] => Sun [icon] => icons/m/mostly_sunny.gif [condition] => Mostly Sunny [high] => Array ( [f] => 34 [c] => 1 ) [low] => Array ( [f] => 23 [c] => -5 ) ) ) )
This is a no-charge service and as such, Google can take it down or change it at anytime without notice. With that in mind, use it wisely.
6 Comments
Thank Your script its very helpfull, the one i'm using with some changes. Coz i made custom localization.
Very cool, Marcel. Let me know how it works out.
Omar Maya, 2 months ago
Hey Omar. Thanks so much for this script. I'm trying to get it working as a test but getting "No data available..." I have GD installed and the folder is set to 777. Any ideas why it wouldn't be working? Any help is appreciated. Thanks!
To follow up. I do see that the cache and icons folder was created in my directory but the file has the following in it:
!valid(http://www.google.com/ig/api?weather=mexico+city)
Dave, 1 month ago
ok... figured it out. you were hiding the error thrown with the @ symbol on this line
if($file_string = file_get_contents($external_url)) {
file_get_contents had been disabled on server but I wasn't getting an error about that.
adding this to my php.ini file fixed it.
allow_url_fopen = On
Thanks!
Glad you figured it out, Dave! Thanks for sharing.
Omar Maya, 1 month ago
What's on your mind?
Gravatar