Skip to content

Instantly share code, notes, and snippets.

@clarklab
Created January 27, 2012 21:49
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 clarklab/1691109 to your computer and use it in GitHub Desktop.
Save clarklab/1691109 to your computer and use it in GitHub Desktop.
Using get_transient and set_transient to save changing data
<?php
//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("http://api.klout.com/1/klout.json?key=[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