Skip to content

Instantly share code, notes, and snippets.

@RadGH
Created May 12, 2014 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RadGH/4c27fe94c53969ec1901 to your computer and use it in GitHub Desktop.
Save RadGH/4c27fe94c53969ec1901 to your computer and use it in GitHub Desktop.
Get the current temperature and weather summary from Forecast.io, cached results
<?php
$recent_weather = get_transient('recent_weather_widget');
if ( !$recent_weather ) {
$api_key = 'YOUR_API_KEY';
$lat = '44.052303';
$long = '-123.102148';
$cache_duration = 60*30; // seconds
// retrieve weather information from forecast.io
$json = file_get_contents( sprintf('https://api.forecast.io/forecast/%s/%s,%s', $api_key, $lat, $long) );
if ( !$json ) return;
// decode the information into an object
$recent_weather = json_decode($json);
if ( !$recent_weather ) return;
// store the results
set_transient('recent_weather_widget', $recent_weather, $cache_duration);
}
if ( !$recent_weather->currently->temperature ) return;
?>
<div id="weather" title="Weather for Eugene, OR">
<a href="/weather/"><span class="temp"><?php echo round($recent_weather->currently->temperature); ?>&deg;F</span> <span class="weather"><?php echo $recent_weather->currently->summary; ?></span></a>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment