Skip to content

Instantly share code, notes, and snippets.

@reinink
Created November 25, 2011 03:42
Show Gist options
  • Save reinink/1392767 to your computer and use it in GitHub Desktop.
Save reinink/1392767 to your computer and use it in GitHub Desktop.
PHP wrapper for Google Weather API
<?php
class Google_Weather
{
public $condition;
public $temperature;
public $humidity;
public $icon;
public $wind_direction;
public $wind_speed;
public function load($city)
{
// Pull from API and parse XML
$data = simplexml_load_string(utf8_encode(file_get_contents('http://www.google.com/ig/api?weather=' . urlencode($city))));
// Set current conditions
$this->condition = (string) $data->weather->current_conditions->condition['data'][0];
$this->temperature = (string) $data->weather->current_conditions->temp_c['data'][0];
$this->humidity = substr($data->weather->current_conditions->humidity['data'][0], 10);
$this->icon = substr($data->weather->current_conditions->icon['data'][0], 19, -4);
// Set wind direction
preg_match('/Wind: (.*?) at/', $data->weather->current_conditions->wind_condition['data'][0], $matches);
$this->wind_direction = $matches[1];
// Set wind speed (convert to km/h)
preg_match('/at (.*?) mph/', $data->weather->current_conditions->wind_condition['data'][0], $matches);
$this->wind_speed = floor($matches[1] * 1.6);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment