Posted By

rodrigo on 09/03/08


Tagged

rss xml cache twitter filegetcontents serialize unserialize


Versions (?)

Twitter Cache


 / Published in: PHP
 

URL: http://reactor.cl

Place this code where you like and make relative directory 'cache' writable.

Set your Twitter userid by defining it with $usuarioid = 'your twitter id';

Parse your Twitter RSS as you like using $rss

  1. $cache_file = dirname(__FILE__).'/cache/twitter_cache';
  2.  
  3. /* Start with the cache */
  4.  
  5. if(file_exists($cache_file))
  6.  
  7. {
  8.  
  9. $mtime = time_diff(filemtime($cache_file), time());
  10.  
  11. if($mtime['minutes'] > 9)
  12.  
  13. {
  14.  
  15. $cache_rss = file_get_contents('http://twitter.com/statuses/user_timeline/'.$usuario_id.'.rss');
  16.  
  17. $cache_static = fopen($cache_file, 'wb');
  18.  
  19. fwrite($cache_static, serialize($cache_rss));
  20.  
  21. fclose($cache_static);
  22.  
  23. }
  24.  
  25. echo "<!-- cache de twitter generado el ".date('Y-m-d h:i:s', filemtime($cache_file))." -->";
  26.  
  27. }
  28.  
  29. else
  30.  
  31. {
  32.  
  33. $cache_rss = file_get_contents('http://twitter.com/statuses/user_timeline/'.$usuario_id.'.rss');
  34.  
  35. $cache_static = fopen($cache_file, 'wb');
  36.  
  37. fwrite($cache_static, serialize($cache_rss));
  38.  
  39. fclose($cache_static);
  40.  
  41.  
  42.  
  43. $rss = $cache_rss;
  44.  
  45. }
  46.  
  47. /* End of caching */
  48.  
  49. $rss = @unserialize(file_get_contents($cache_file)); // Now parse it as you'd like

Report this snippet  

You need to login to post a comment.