Posted By

leonard_mocanu on 04/13/11


Tagged

php cache filegetcontents


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

mikael12


Cache


 / Published in: PHP
 

  1. <?php
  2.  
  3. // The location of the cache file
  4. $cache_file = '/tmp/tweetmeme';
  5.  
  6. $url = 'http://api.tweetmeme.com/url_info';
  7.  
  8. // If the file doesn't exist or 1 hour has passed since the last update, write the new data to the file
  9. if(!file_exists($cache_file) OR (time() - filemtime($cache_file) > 3600)) {
  10.  
  11. $data = file_get_contents($url);
  12. $data = trim($data);
  13.  
  14. $xml_string = simplexml_load_string($data);
  15.  
  16. // If there is data and the data is XML, write it
  17. if($data != '' AND $xml_string !== FALSE) {
  18.  
  19. file_put_contents($cache_file, $data);
  20.  
  21. }
  22.  
  23. } else {
  24.  
  25. $data = file_get_contents($cache_file);
  26.  
  27. }
  28.  
  29. echo $data;

Report this snippet  

You need to login to post a comment.