Skip to content

Instantly share code, notes, and snippets.

@oknoway
Created April 3, 2011 03:05
Show Gist options
  • Save oknoway/900143 to your computer and use it in GitHub Desktop.
Save oknoway/900143 to your computer and use it in GitHub Desktop.
function getWeather() {
if (false === ( $theWeather = get_transient('theWeather') ) ) {
$xmlUrl = 'http://www.google.com/ig/api?weather=portland&oe=utf-8&';
$output = wp_remote_fopen($xmlUrl);
$xmlData = simplexml_load_string($output);
$conditions = $xmlData->weather->current_conditions->condition['data'];
$raining = array( 'Showers', 'Scattered Showers', 'Chance of Rain', 'Chance of Storm', 'Rain', 'Light Rain' );
$cloudy = array( 'Overcast', 'Cloudy' );
$partly_cloudy = array( 'Partly Cloudy', 'Mostly Cloudy', 'Mist', 'Dust', 'Fog', 'Smoke', 'Haze' );
$sunny = array( 'Sunny', 'Clear', 'Partly Sunny', 'Mostly Sunny' );
$lightning = array( 'Scattered Thunderstorms', 'Storm', 'Thunderstorm', 'Chance of Tstorm' );
$snowing = array( 'Rain and Snow', 'Light Snow', 'Freezing Drizzle', 'Chance of Snow', 'Sleet', 'Snow', 'Icy', 'Flurries' );
if ( in_array( $conditions, $raining ) ) :
$theWeather = 'raining';
elseif ( in_array( $conditions, $cloudy ) ) :
$theWeather = 'cloudy';
elseif ( in_array( $conditions, $partly_cloudy ) ) :
$theWeather = 'partly-cloudy';
elseif ( in_array( $conditions, $sunny ) ) :
$theWeather = 'sunny';
elseif ( in_array( $conditions, $lightning ) ) :
$theWeather = 'lightning';
elseif ( in_array( $conditions, $snowing ) ) :
$theWeather = 'snowing';
else :
$theWeather = 'raining';
endif;
set_transient('theWeather', $theWeather, 60*30);
}
return $theWeather;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment