Local weather with PHP and Google Weather

How to display local weather with Google Weather API and PHP Local weather can be great content on a website or blog.

There are many different services that provides local weather.

In this post we’ll build a small PHP script that uses the Google weather API.

Google Weater API

Google weather API is a API that isn’t mentioned on the official Google API pages. The API more or less became visible when Google launched iGoogle, so my guess is, that the API has been launched for supporting gadgets for iGoogle. This theory can be supported by the fact that the URL to the API contains the  letters “ig”.

The unofficial status of the API also means that documentation and support is limited, so it’s on your own risk if you use the API at a staging environment of a website.

Accessing the API is simple – just include a location in the request, and then you’ll get the current conditions and a  5 day forecast in return.

The 5 day forecast contains the following informations:

  • condition
  • low = lowest temperature during the day
  • high = highest temperature during the day
  • day of week = name of the weekday
  • img = graphical representation of the forecast

Creating the PHP code

To integrate the API in a website, you need to use the CURL PHP function, since the API won’t answer a direct request from a script.

The script below takes the $place parameter and passes it to the Google Weather API, and then parses the API answer with simple XML and outputs it to the browser.

<?
$placename = 'new york'; // city where you want local weather
$place=urlencode($placename);
$place = utf8_encode($place);
$url = 'http://www.google.com/ig/api?weather='.$place.',$&hl='.$lang.'';
$ch = curl_init();
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
$raw_data = curl_exec ($ch);
curl_close ($ch);

$xml = simplexml_load_string($raw_data);
$condition = $xml->weather->current_conditions->condition['data'];
$temp_c = $xml->weather->current_conditions->temp_c['data'];
$humidity = $xml->weather->current_conditions->humidity['data'];
$icon = $xml->weather->current_conditions->icon['data'];

echo ("<h1>Local weather for $placename</h1>");

for ($i = 0; $i < count($xml->weather->forecast_conditions); $i++){
 $data = $xml->weather->forecast_conditions[$i];
 $day_of_week = $data->day_of_week['data'];
 $low = $data->low['data'];
 $high = $data->high['data'];
 $condition = $data->condition['data'];
 $day_of_week = utf8_decode($day_of_week);
 $img = 'http://img0.gmodules.com/' . $data->icon['data'];
 echo ("$day_of_week<br/><img src=\"$img\"/>$low&#176;|$high&#176;<br/>$condition<br/><br/>");
}
?>

See example here

Depending on the users location, the script outputs temperature data in either Celsius or Fahrenheit.

Conclusion

The Google Weather API gives great weather forecasts all around the world.

It’s therefore a pity, that it’s not a official API.

As you can see from this post, the API works fine and has been working fine for the last 3 years, but if you want to use it in a production environment is up to you – no guarantees from me or Google.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS
fold-left fold-right
About the author
Jørgen Nicolaisen has been passionately interested in everything online since 1995. His experience is based on working with small hobby projects as well as high volume websites. Jørgen is currently focused on the PHP based programming framework - Codeigniter, and WordPress naturally

12 Replies to Local weather with PHP and Google Weather

  1. morrers says:

    I like it but cities with 2 words dont work !

  2. jhnidk says:

    Hi,

    You properly needs to look into the encoding – eg. UTF-8 encode the city.

  3. sunny says:

    can you please make it encoded for the city New York by UTF-8 I will be thankful

  4. jhnidk says:

    Good point. The script is now updated so it encodes special characters with utf8, and also handles spaces in city names in two words with url encoding.

  5. sunny says:

    hi jhnidk thanks alot for your kind support and quick response , it is working but one thing also I let to know that it is showing the week days from today to coming monday . Is it possible to show the forecast for the next seven days where I have to change for this as I am newbie in this , hope you will respond soon .

    Thanks in Advance
    Sunny

  6. sunny says:

    hi jhnidk , as you have written ‘If you want the temperatures in celsius, you should replace: “http://www.google.com/ig/api…” in line 4 with

    “http://www.google.co.uk/ig/api…”’ I changed it but giving the same results in farenheit.

  7. jhnidk says:

    Unfortunately this is how the service is working, and to my knowledge, there isn’t any way this can be changed

  8. jhnidk says:

    Hi,

    I investigated this further, and it looks like the API is returning temperature unit based on the users location. The exception is the current condition, that’s both returned as Celsius and Fahrenheit.

  9. sunny says:

    thank you jhnidk for kind support and helping the developers , wish you good luck for giving such type of materials

  10. Emile says:

    Thank you for the example. One remark: The .$lang. variable is not defined in the program.

  11. sorio says:

    Is there any solution for change images? Thanks for the great article!

Trackbacks for this post

  1. The secret Google Stock API | Tips4PHP

Comments are now closed for this article.