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
Pretty nice post. I just stumbled upon your blog and wanted to say that I have really enjoyed browsing your blog posts. In any case I’ll be subscribing to your feed and I hope you write again soon!
I’ve been interested in this recent update and looks like I will look for some additional infomation. You really interested me.
That’s fine and dandy for the US, but I am having some trouble figuring out the parameters for international locations.
Any ideas?
Found a solution
http://www.google.com/ig/api?weather=,,,4000000,-7100000
That allows you to add international data using lat & long instead of guessing how google adds the city name in their database
Rupen, Thanks for the comment. I did not know that the API would take Lat/Long cords. Good find!
You can give the API non-US city/country pairs as well.
Example: http://www.google.com/ig/api?weather=paris,france
Yes, but it gets very complicated when you pair up remote cities in remote countries.
The LAT/LONG approach is 100$ fullproof method