Created
January 5, 2011 15:43
-
-
Save mhawksey/766473 to your computer and use it in GitHub Desktop.
script to cache twitter timeline (modified from http://brenelz.com/blog/build-a-php-twitter-widget/)
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 | |
function twitterCapture($type) { | |
// Set your username and password here | |
$user = 'rsc_ne_scotland'; // Twitter Username | |
if ($type == "json") $type .= "?callback=twitterCallback2&count=8"; | |
$tw = curl_init("http://twitter.com/statuses/user_timeline/{$user}.$type"); //grabs the JSON format of your timelines | |
curl_setopt($tw,CURLOPT_TIMEOUT, 30); // Timeout (for when Twitter is down) | |
curl_setopt($tw,CURLOPT_RETURNTRANSFER,1); // returns | |
$json = curl_exec ($tw); // Executes the cURL and puts it into the varible | |
return $json; // returns what was grabbed | |
} | |
function checkandWrite($type){ | |
$cachefile = 'cache/JISCRSCScotNE.'.$type; // the location to your cache file | |
$cachetime = 10 * 60; // set the cach time 10 * 60 (1 hour) | |
// if the file exists // if and the time it was created is less then cache time | |
if ( (file_exists($cachefile)) && ( time() - $cachetime < filemtime($cachefile) ) ) { | |
// script will not use the cached version | |
}else{ // the file didn't exist or is old | |
ob_start(); // turn on the output buffering | |
$fp = fopen($cachefile, 'w'); // opens for writing only or will creat it's not there | |
fwrite($fp, twitterCapture($type)); // writes to the file what was grabbed from the previouse function | |
fclose($fp); // closes | |
ob_end_flush(); // finishes and flushes the output buffer | |
} | |
} | |
checkandWrite("json"); | |
checkandWrite("rss"); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment