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
  • Clorinda Trexler

    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

  • Michael Philip

    Thanks Ashley!

    Works as expected and 100% cross browser compatibility…

    Keep it up!

  • Brian Borup

    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 0x77 0x2C 0x20 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

  • Devin

    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’].'”/>'; ?>”

    • Devin

      EDIT: The code didn’t show :P

      Change “img src=” to “img src=”” :D