PHP Weather widget using Google’s weather API

May 17 2012
PHP Weather widget using Google’s weather API

UPDATE 08/28/2012

Google has abandoned their weather API and are now using wunderground.com Weather API. I will be creating a new tutorial showing the API in action very soon. Check back to the RD2 blog for updates. Please disregard the below code.


With the recent launch of the Elbowz Racing team website, we decided to add something a little bit fun, but still applicable, into the project: a self-maintaining weather widget using Google’s weather API and PHP. The cycling team, much like their sponsor Ben Spies, is always moving around for races, and we thought it would be cool for visitors to quickly see where the Elbowz team is. To accomplish this, we placed the weather widget on the home page, where it updates automatically based on their current race location.

The Elbowz team enters their racing schedule into the WordPress admin console at the beginning of each season. If a race is scheduled to occur on the date the visitor loads the website, a function sends the location specified for the race to Google Maps and then receives the response.

Next we would take the response, process it to determine what’s needed, and display the useful data within a CSS-styled module on the homepage, with an image that visually indicates the current weather conditions at the racing location. Below is the PHP function used to gather the data:

/*
 * @description    Gets information using Google's Weather API and returns an array with the weather information
 * @param          String containing the location.
 * @return         Array
 */

function get_weather_from_city( $location ) {
    // load the XML feeds for the Google Weather API
    $xml = simplexml_load_file('http://www.google.com/ig/api?weather='.urlencode( $location ));
    $current = $xml->xpath("/xml_api_reply/weather/current_conditions");
    $forecast = $xml->xpath("/xml_api_reply/weather/forecast_conditions");

    $weather = array();

    //Get latitude and longitude by city and sunrise
    $now = time();

    $location = str_replace(" ", "+", $location);
    $region = 'united+states';
    $json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$location&sensor=false®ion=$region");
    $json = json_decode($json);

    $lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
    $long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};

    $sunrise = date_sunrise($now, SUNFUNCS_RET_TIMESTAMP, $lat, $long, 90.583333, 0);
    $sunset = date_sunset($now, SUNFUNCS_RET_TIMESTAMP, $lat, $long, 90.583333, 0);

    if (($now > $sunrise) && ($now < $sunset)){ //day                     $weather['time'] = 'day';       }else{ //night          $weather['time'] = 'night';     };     // do a basic error check to see if we can get the current weather condition for the given location     // if no return an error.     if(!$current[0]->condition['data']){
        $error = "Couldn't determine this location";
        $weather['error'] = $error;
    }
    $weather['current_tempature'] = $current[0]->temp_f['data'];
    $weather['weather'] = strtolower($current[0]->condition['data']);
    return $weather;
}

3 Comments to “PHP Weather widget using Google’s weather API”

  1. Hi, Today I discovered that the weather google api cannot be used. They treat thee request to get the xml file as an automated query, and just outputs this message: We’re sorry… … but your computer or network may be sending automated queries. To protect our users, we can’t process your request right now.

    By CoursesWeb on August 28th, 2012 at 4:07 am
  2. Thank you CoursesWeb for pointing this out. It looks like Google has quietly killed their weather API and is now using Wunderground’s weather API. http://www.wunderground.com/weather/api/

    By Colin Mitchell on August 28th, 2012 at 10:07 am
  3. Pretty great post. I simply stumbled upon your blog and wanted to mention that I’ve truly loved browsing your weblog posts. After all I’ll be subscribing in your rss feed and I am hoping you write again very soon!

    By obat tbc on March 30th, 2013 at 1:35 pm

Leave a Reply