Header add "Expires" "Mon, 28 Jul 2014 23:30:00 GMT" Header add "Cache-Control" "max-age=31536000" EOD; fwrite($f, $htaccess); fclose($f); } } $photo_urls = array(); $cache_urls = array(); // open cache directory and get contents if(file_exists('cache') && is_dir('cache') && ($dir = opendir('cache'))) { $id = 0; while(false !== ($file = readdir($dir))) { if($file !== "." && $file !== ".." && $file !== ".htaccess") { $cache_urls[$id] = $file; $id++; } } closedir($dir); } else { die("Cannot find cache directory"); } if(!file_exists('.timestamp') || (time() - filemtime('.timestamp')) > 3600) { touch('.timestamp'); // size=s produces a square thumbnail (75x75) $jsSource = file_get_contents('http://www.flickr.com/badge_code_v2.gne?count=8&display=latest&source=user&size=s&user=20907252%40N00'); $match_string = '/ 0) { $id = 0; while($id < $photo_count) { $photo_urls[$id] = $matches[1][$id]; $id++; } } // compare file URLs to cache URLs and determine the ones we need to fetch for($i = 0; $i < $photo_count; $i++) { $filename = basename($photo_urls[$i]); if( false !== ($index = array_search($filename, $cache_urls)) ) { // remove $index from the list of cached urls unset($cache_urls[$index]); } else { // we need to fetch this image from Flickr // and now store it in our cache $fileToWrite = fopen("cache/" . $filename, 'w'); if($fileToWrite) { fwrite($fileToWrite, file_get_contents($photo_urls[$i])); fclose($fileToWrite); } } } // anything left in $cache_urls is stale and we should remove foreach($cache_urls as $file_to_remove) { unlink('cache/' . $file_to_remove); } // close curl resource, and free up system resources // curl_close($ch); // convert to local URLs foreach($photo_urls as $ind => $photo) { $photo_urls[$ind] = 'cache/' . basename($photo); } } else { // convert to local URLs foreach($cache_urls as $ind => $photo) { $photo_urls[$ind] = 'cache/' . basename($photo); } } // Now construct the SVG+SMIL with the URLs // http://www.flickr.com/badge.gne header('Content-Type: image/svg+xml');?>