3 months ago on February 9
Download free source files for this article. Example

Google Weather

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.

Google Weather API

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.

The PHP Class

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.

What It Features
  • 15-minute location feed caching (even for locations not found)
  • 1-week weather icon caching
  • Measurements converted and added for metric system
  • Normalizes search location to non-accent characters before sending for optimum search
What's Necessary (Download Link Above)
  • google.weather.class.php
  • resize.class.php (Image resizer class by Jarrod Oberto)
  • GD Graphics Library PHP module installed
  • Write access

The Example

The End Result
Rain
Paris, Île-de-France

Rain - 18°F - Wind: N at 2 mph - Humidty: 93%

Forecast date: 2/9/2012 07:22 UTC

Rain

Thu - Partly Sunny: 19°F/34°F

Rain

Fri - Mostly Sunny: 19°F/34°F

Rain

Sat - Mostly Sunny: 16°F/32°F

Rain

Sun - Mostly Sunny: 23°F/34°F

Get All Available Data

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.

<?php
 
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';
}
 
?>
The Result
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
						)
				)
		)
)

Some Notes

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.

Short URL: http://mayavps.com/a20
If you enjoyed this post, please subscribe via e-mail, RSS, or Twitter.

6 Comments

marcel

marcel, 3 months ago
Côte d'Ivoire Windows Firefox

Thank Your script its very helpfull, the one i'm using with some changes. Coz i made custom localization.

0

Very cool, Marcel. Let me know how it works out.

 
Omar Maya

Omar Maya, 2 months ago

Mexico Windows Firefox

Dave

United States Macintosh Safari

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!

0

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)

0

Dave

United States Macintosh Safari

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!

0

Glad you figured it out, Dave! Thanks for sharing.

 
Omar Maya

Omar Maya, 1 month ago

Mexico Windows Firefox

What's on your mind?

Gravatar

HTML not converted. Links begin with http://. Code blocks should be wrapped with [code][/code]. Unicode characters accepted.


E-Mail RSS