Created
April 30, 2012 19:35
-
-
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.
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 | |
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