Skip to content

Instantly share code, notes, and snippets.

@miketucker
Created April 30, 2012 19:35
Show Gist options
  • Save miketucker/2561938 to your computer and use it in GitHub Desktop.
Save miketucker/2561938 to your computer and use it in GitHub Desktop.
Caches a json file of your twitter timeline with a 10 minute timeout. Helpful to combat the API limit.
<?php
header('Content-type: text/json');
header('Content-type: application/json');
function savetwit ($username)
{
//create the rss feed
$local_file = './twitterfeeds/'.$username.'.json';
$url = "https://api.twitter.com/1/statuses/user_timeline.json/?screen_name=".$username."&include_entities=true";
if (is_file($local_file))
{
//Find out how many seconds it has been since the file was last updated - in seconds
$time_lapse = (strtotime("now") - filemtime($local_file));
//if it has been more than 10 minutes, update the local feed
if ($time_lapse > 600)
{
//grab the feed from twitter
$feed_grab = file_get_contents($url);
//check to make sure the feed has content and isn't just some error message
if (strlen($feed_grab) > 100)
{
//if all looks good, update the local feed
file_put_contents($local_file, $feed_grab);
echo $feed_grab;
}
} else {
echo file_get_contents($local_file);
}
}
else
{
//grab the feed from twitter
$feed_grab = file_get_contents($url);
//check to make sure the feed has content and isn't just some error message
if (strlen($feed_grab) > 100)
{
//if all looks good, update the local feed
file_put_contents($local_file, $feed_grab);
echo $feed_grab;
}
}
}
$u = 'YOUR TWITTER SCREENNAME';
savetwit($u);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment