Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Using get_transient and set_transient to save changing data
//let's get my klout score!!
//first, let's see if I've got the data already cached locally
$klout = get_transient("klout");
//dang it! it looks like I might not. let's check with the Klout API
if( !$klout ) {
$json = file_get_contents("[API key]&users=clarklab");
$return = json_decode($json, true);
$kloutscore = $return["users"][0]["kscore"];
//if the Klout API gave us a score, let's save it!
if( $kloutscore ) {
//in a field named "klout", we'll save our $kloutscore, for 12 hours (60 seconds x 60 minutes x 12 hours)
set_transient("klout", $kloutscore, 60 * 60 * 12);
//let's also make sure to set the data you were originally looking for
$klout = $kloutscore;
echo 'Now I can safely check my '.$klout.' score with transients!';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment