Google Weather API

Posted by Tully on Mon 04 October 2010

Today I needed to display weather results for given cities. After reading reviews and API docs from various sites, I decided to use the Google Weather API. The Google Weather API was very easy to work with. All you need to do is hit the API with your city/zip and Google would return the 4 day forecast in XML.

Example of how to get the current condition:

<?php
$weatherObj = new GoogleWeather('Los Angeles California');  
$weatherResult = $weatherObj->getCurrentCondition ();

Example of how to retrieve 4 day forecast:

<?php
$weatherObj = new GoogleWeather('Los Angeles California');  
$weatherResult = $weatherObj->getAllDays();

Google Weather API Class

<?php
/**
* Used to return Weather results from Googles Weather API.
*
* Requires (PHP Curl,PHP Tidy)
*
* @author Tully Rankin
*
*/
class GoogleWeather {

    /**
    * URL of API
    *
    * @param string
    */
    protected $url;

    /**
    * Search Query
    *
    * @param string
    */
    protected $query;

    /**
    * Response from API Query
    *
    * @param string
    */
    protected $data;

    /**
    * DOM Instance 
    *
    * @param DOMDocument
    */
    protected $dom;

    /**
    * Initializes GoogleWeather properties and
    * queries API with search string.
    *
    * @param string $query Location to search
    * @param string $url URL of the Google Weather API
    */
    public function __construct($query, $url = 'http://www.google.com/ig/api?weather=')
    {
        $query = str_replace(' ', '+', $query);
        $this->query = $query;
        $this->url = $url;
        $this->_retrieveDATA();
        $this->dom = new DomDocument();
        $this->dom->loadXML($this->data);
        if ($this->_isResultEmpty())
            return false;
    }

    /**
    * Queries GoogleWeather API.
    * uses PHP Curl extention.
    */
    protected function _retrieveDATA()
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->url . $this->query);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($ch);
        $this->data = $this->_tidyXML($data);
    }

    /**
    * Cleans up malformed XML Data.
    *
    * @param string $xml XML Formatted string
    * @return string
    */
    protected function _tidyXML($xml)
    {
        $options = array('input-xml' => true, 'output-xml' => true, 'clean' => true);
        $clean = tidy_parse_string($xml, $options, 'UTF8');
        tidy_clean_repair($clean);
        return $clean;
    }

    /**
    * Checks if returned DATA has results.
    *
    * @return bool 
    */
    protected function _isResultEmpty()
    {
        $error = $this->dom->getElementsByTagName('problem_cause')->item(0);
        return $error;
    }

    /**
    * Gets the current weather condition.
    *
    * @return array
    */
    public function getCurrentCondition()
    {
        $node = $this->dom->getElementsByTagName('current_conditions')->item(0);
        $current = array();
        foreach ($node->childNodes as $c) {
            $current[$c->nodeName] = $c->getAttribute('data');
        }
        return $current;
    }

    /**
    * Get weather conditions for all days. As of now
    * Google currently returns 4 days.
    *
    * @return array
    */
    public function getAllDays()
    {
        $nodes = $this->dom->getElementsByTagName('forecast_conditions');
        $i = 1;
        $weather = array();
        foreach ($nodes as $node) {
            foreach ($node->childNodes as $c) {
                if ($c->hasAttributes())
                    $weather[$i][$c->nodeName] = $c->getAttribute('data');
            }
            $i++;
        } 
        return $weather;
    }
}