Posted By

calebrown on 03/31/09


Tagged

cache twitter


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

calebrown
maxvee8
muoto
ncho
BrianCoyDesign


Twitter Cache


 / Published in: PHP
 

URL: http://calebrown.com

This is a simple function that will Cache twitter rss feeds locally so you don't max out your API limit.

  1. function savetwit ($username)
  2. {
  3. //create the rss feed
  4. $local_file = './twitterfeeds/'.$username.'.rss';
  5.  
  6. if (is_file($local_file))
  7. {
  8. //Find out how many seconds it has been since the file was last updated - in seconds
  9. $time_lapse = (strtotime("now") - filemtime($local_file));
  10. //if it has been more than 10 minutes, update the local feed
  11. if ($time_lapse > 600)
  12. {
  13. //grab the feed from twitter
  14. $feed_grab = file_get_contents('http://twitter.com/statuses/user_timeline/'.$username.'.rss');
  15. //check to make sure the feed has content and isn't just some error message
  16. if (strlen($feed_grab) > 100)
  17. {
  18. //if all looks good, update the local feed
  19. file_put_contents($local_file, $feed_grab);
  20. }
  21.  
  22.  
  23. }
  24.  
  25.  
  26. }
  27.  
  28. else
  29. {
  30. //grab the feed from twitter
  31. $feed_grab = file_get_contents('http://twitter.com/statuses/user_timeline/'.$username.'.rss');
  32. //check to make sure the feed has content and isn't just some error message
  33. if (strlen($feed_grab) > 100)
  34. {
  35. //if all looks good, update the local feed
  36. file_put_contents($local_file, $feed_grab);
  37. }
  38.  
  39. }
  40.  
  41. }

Report this snippet  

You need to login to post a comment.