Use your left/right keys to browse tutorials
Using Google’s Weather API

Using Google’s Weather API

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on October 25, 2011

I thought it would be a nice little project to write a tutorial on how to build a basic but powerful weather application for either mobile devices or the desktop. The application takes a set of Google weather API feeds and makes some simple decisions based on the output. For example we can recommend the user takes an umbrella if rain or storms are forecast, or whether the temperature warrants the need for warm clothes. Basic but nonetheless useful information.

IMPORTANT UPDATE: Google has pulled the plug on this undocumented Weather API. you can read more about it’s demise here on The Next Web

The Logic Code

Lets have a quick run through at what the project consists of. Firstly we use the simplexml_load_file() PHP function to get the xml data for the given location. We then check to see if the current weather condition is in the XML file. If it doesn’t exist then we can make the assumption that the API feed couldn’t determine the location, bear in mind this is very basic error checking and if the feed fails it will still return this error.

We now do some basic data checking to to see what extra data we can give the user. Firstly we check on line 14 to see if the current weather and forecast are different, if they are different we’ll display the forecast if not we show just the current weather.

Next on lines 21 and 28 we do a simple check to see if the temperature is higher or lower than 20 degrees and echo whether it’s likely to be hot or cold.

Finally on lines 35 we check to see if the current or future forecast contains any rain or storms so we can recommend the user takes an umbrella. This is done using some basic PHP regular expression.

	// load the XML feeds for the Google Weather API
	$xml = simplexml_load_file(''.urlencode($_GET['location']));
	$current = $xml->xpath("/xml_api_reply/weather/current_conditions");
	$forecast = $xml->xpath("/xml_api_reply/weather/forecast_conditions");

	// do a basic error check to see if we can get the current weather condition for the given location
	// if no return an error.
		$error = 'Couldn\'t determine this location';
	// is the current weather the same as the forecast? if not display the forecast
	$outlook = 'but the forecast says '.strtolower($forecast[0]->condition['data']);	
	// if the temp in degrees c is below 20 i.e. cold
	$coat = 'If you\'re going outside i\'d wrap up warm.';	
	// if the temp in degrees c is over 21 i.e. Warm / Hot	
	$coat = 'You should be ok without warm clothes today.';	
	// check to see if there is rain or storms forecast
	if (preg_match("/\brain\b/i", $current[0]->condition['data']) || 
		preg_match("/\brain\b/i", $forecast[0]->condition['data']) ||
		preg_match("/\bstorm\b/i", $current[0]->condition['data']) ||
		preg_match("/\bstorm\b/i", $forecast[0]->condition['data'])
		$umbrella = ' But <u>don\'t forget to take an umbrella</u>!';

Displaying the data

<form action="" method="get">
<label for="location">Location</label>
<input type="text" class="location" name="location" value="<?php echo $_GET['location'];?>"/>

<?php if(!empty($_GET['location'])){
	if($error){ echo '<div class="errors">'.$error.'</div>'; }else{

<div class="weather_app">

<h1>Weather Summary for <?php echo $_GET['location'];?></h1>

<li><?php echo '<img src="'.$current[0]->icon['data'].'"/>'; ?> The weather in <?php echo $_GET['location'];?> is <strong><?php echo strtolower($current[0]->condition['data']).' '. $outlook;?></strong>. The temperature is currently <strong><?php echo $current[0]->temp_c['data']; ?>&deg;c (<?php echo $current[0]->temp_f['data']; ?>&deg;f)</strong>. <?php echo $coat;?> <?php echo $umbrella;?></li>


<?php } } ?>

More tutorials from Papermashup
28 discussions around Using Google’s Weather API
Older Comments
  1. Nice post. I study something more challenging on completely different blogs everyday. It is going to all the time be stimulating to learn content from different writers and apply a bit of something from their store. I

  2. Michael Philip says:

    Thanks Ashley!

    Works as expected and 100% cross browser compatibility…

    Keep it up!

  3. Brian Borup says:

    I’ve been trying out several Google Weather API solutions the last few weeks, but they all seem to fail sometimes when I refresh the page. Your code does the same thing.
    Any ideas om how to prevent this?

    This is the error message on your page:

    Warning: simplexml_load_file() [function.simplexml-load-file]: parser error : Input is not proper UTF-8, indicate encoding ! Bytes: 0xF3 0×77 0x2C 0×20 in /nfs/c02/h11/mnt/40992/domains/ on line 21

    Warning: simplexml_load_file() [function.simplexml-load-file]: pped=”1″ row=”0″ section=”0″ ><city data="Gmina Głog in /nfs/c02/h11/mnt/40992/domains/ on line 21

    Warning: simplexml_load_file() [function.simplexml-load-file]: ^ in /nfs/c02/h11/mnt/40992/domains/ on line 21

    Fatal error: Call to a member function xpath() on a non-object in /nfs/c02/h11/mnt/40992/domains/ on line 22

  4. Devin says:

    Hi! I found an error in your code that prevents the icons from showing.

    You need to change “<?php echo 'icon['data'].’”/>’; ?>”
    “<?php echo 'icon['data'].’”/>’; ?>”

Older Comments

Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.

Get in contact

Please use the form below to get in contact. If your question is related to a free script download, please use the comments on the article page as community members are more likely to respond quicker than I can personally.

About Me

I'm Ashley Ford, Co-founder and Technical Director at London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.

What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on please get in touch via the contact link below for advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.