Google offer a whole range of API’s for web developers, Google Maps and Google Checkout being just 2 popular options which are well supported and widely implemented.
I am currently redeveloping Farmores Hotel and bed and breakfast site Places2stay. the site itself works as an affiliate portal site for Late Rooms, basically, users can search for hotels with Places2stay which queries the Late Rooms XML feed and the simplexml engine returns the result, have a go searching for something …. search Places2stay for hotels in Glasgow.
So that’s a short background to my little story. we wanted to show the current weather for each loction on the site, this had to be dynamic and accurate. There are a few options available such as the widgets offered by the Met office and also Yahoo weather which offers an XML feed, unfortunatley that required an API key and only gave information for the curernt day (We wanted the forecast for the next few days). So i happened accross Googles Weather API which is primarily for use by Google for iGoogle.
Google haven’t offered the API as a service to the public and don’t support it, so apart from some blog posts on the subject there’s not a whole lot of info about it. this means that the XML feed could e withdrawn at any time and any changes to the service wouldn’t be publicised, there’s an informitive post about those issues here.
It’s a simple REST call which takes this form http://www.google.com/ig/api?weather=Glasgow
And that call returns the following XML……
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<?xml version="1.0"?>
<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="Glasgow, Lanarkshire"/>
<postal_code data="Glasgow,uk"/>
<latitude_e6 data=""/>
<longitude_e6 data=""/>
<forecast_date data="2010-08-28"/>
<current_date_time data="2010-08-28 21:20:00 +0000"/>
<unit_system data="US"/>
</forecast_information>
<current_conditions>
<condition data="Cloudy"/>
<temp_f data="55"/>
<temp_c data="13"/>
<humidity data="Humidity: 82%"/>
<icon data="/ig/images/weather/cloudy.gif"/>
<wind_condition data="Wind: W at 9 mph"/>
</current_conditions>
<forecast_conditions>
<day_of_week data="Sat"/>
<low data="50"/>
<high data="60"/>
<icon data="/ig/images/weather/mostly_sunny.gif"/>
<condition data="Partly Sunny"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="Sun"/>
<low data="39"/>
<high data="60"/>
<icon data="/ig/images/weather/chance_of_rain.gif"/>
<condition data="Chance of Rain"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="Mon"/>
<low data="39"/>
<high data="60"/>
<icon data="/ig/images/weather/sunny.gif"/>
<condition data="Clear"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="Tue"/>
<low data="42"/>
<high data="66"/>
<icon data="/ig/images/weather/mostly_sunny.gif"/>
<condition data="Mostly Sunny"/>
</forecast_conditions>
</weather>
</xml_api_reply>
|
That’s a lot of useful info for such a simple call, Google even give you a link to the relevant weather icon. So, now you’ve got the weather forecast how can you manipulate it? Well with PHP and it’s rather nice Curl and Simplexml support. The first allows you to consume info from remote addresses the second as its name suggests lets you parse XML quickly.
The following code lets you call the API, consume the information and echo it to the screen, it’s pretty basic and can be adapted easily and improved upon for your needs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?php
$town= 'Glasgow'; //the place you want the weather for
$ch = curl_init();// initiate curl so we can consume the content from the api
$url="http://www.google.com/ig/api?weather=$town,uk";// the url we ar going to parse
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$contents = curl_exec ($ch);
curl_close ($ch);
$s = new SimpleXmlElement($contents, LIBXML_NOCDATA);//use simplexml to parse
foreach($s->weather->forecast_conditions as $item) {// loop through the weather results
$day_of_week = $item->day_of_week[data] ;
$high = $item->high[data] ;
$condition = $item->condition[data] ;
$icon = $item->icon[data] ;
$low = $item->low[data] ;
$high = (5/9)*($high-32);//change from farenheit to celcius
$high = round($high);
$low = (5/9)*($low-32);//change from farenheit to celcius
$low = round($low);
$day= date('l',strtotime($day_of_week));
$weather .= "
<p>
<img src='http://www.google.co.uk$icon' alt='$condition' />//Link to Googles weather icons
<strong>$day</strong><br />
$low°c » $high°c<br />
$condition
</p>
";
}
echo"$weather";
?>
|