* This code is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike License * */ // CONFIGURATION define("_SAVE_PATH", dirname(__FILE__)."/downloaded/"); define("_ERROR_MESSAGE", ""); define("REST_ENDPOINT", "http://www.flickr.com/services/rest/"); if ($fp = @fopen("http://flickr.com", "r")) { // we are online and can see flickr.com - cache any requests that come in fclose($fp); define("_ONLINE", 1); } else { // we can't see flickr.com - try an serve up cached pages instead... define("_ONLINE", 0); } $querystring = array_pop(explode("?", $_SERVER["REQUEST_URI"])); $query_parts = array(); parse_str($querystring, $query_parts); ksort($query_parts); $filename = ""; foreach($query_parts as $key=>$value) { $filename .= $key."|".$value."||"; } $filename = urlencode($filename); if (_ONLINE) { // we are online - go get the info and save it! $fp = fopen(_SAVE_PATH.$filename, "w"); $content = file_get_contents(REST_ENDPOINT."?".$querystring); fwrite($fp, $content); fclose($fp); } else { // we are offline - serve up the saved file if it exists or the error message if you can't find a file if (file_exists(_SAVE_PATH.$filename)) { $content = file_get_contents(_SAVE_PATH.$filename); } else { $content = _ERROR_MESSAGE; } } header("Content-Type: text/xml"); echo $content; ?>