Created
January 27, 2012 21:49
-
-
Save clarklab/1691109 to your computer and use it in GitHub Desktop.
Using get_transient and set_transient to save changing data
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 | |
//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