Created
May 12, 2014 18:31
-
-
Save RadGH/4c27fe94c53969ec1901 to your computer and use it in GitHub Desktop.
Get the current temperature and weather summary from Forecast.io, cached results
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); ?>°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