Google Weather API

Written by Tully on October 4, 2010 Categories: PHP

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. The API also included links to the weather condition images. If you wanted too, you could replace these links easily to use your own custom weather images.

Example of how to get the current condition:

$weatherObj = new GoogleWeather(‘Los Angeles California’);
$weatherResult = $weatherObj->getCurrentCondition ();

Example of how to retrieve 4 day forecast:

$weatherObj = new GoogleWeather(‘Los Angeles California’);
$weatherResult = $weatherObj->getAllDays();

/**
 * 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;
	}

}
// set tabstop=4

Download the Google Weather PHP Class

6 Comments

6 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>