Posted By

jimmysessions on 05/07/09


Tagged

php cache api


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

ikimozu
BrianCoyDesign
fadlee


PHP Remote File Cache


 / Published in: PHP
 

use this to cache a remote file with xml, json etc

  1. $url = 'http://...';
  2. #we need to do some caching here
  3. $cache_dir = dirname(__FILE__) . '/cache/'; // directory to store the cache
  4. $cache_file = $cache_dir . md5($url);
  5. $cache_time = 24 * 60 * 60; // time to cache file, # minutes * seconds
  6.  
  7. // check the cache_dir variable
  8. if(is_dir($cache_dir) && is_writable($cache_dir) && file_exists($cache_file) && time() - $cache_time < filemtime($cache_file)){
  9. $data = file_get_contents($cache_dir . md5($url)); // name of the cached file
  10. } else {
  11. $data = file_get_contents($url);
  12. file_put_contents($cache_dir . md5($url),$data); //go ahead and cache the file
  13. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: incominger on June 11, 2012

really good sample worked like a charm for me. Thank you

Posted By: incominger on June 12, 2012

how can i loop it to echo all "status->text" in xml

You need to login to post a comment.