a stroke of luck

Simulating access to the Flickr API offline


This is a very old page imported from my previous blog. If there is missing content below or anything that doesn’t make sense then please check the page on my old blog.

As I recently mentioned on the Flickr API mailing list, I’m just about to head off on a road trip where I will have my laptop and maybe a little spare time but no Internet connection. And I was thinking it would be good to carry on putting together my Flickr API library for AS2. But – to test my methods requires an internet connection to connect to the Flickr servers… Or does it?

I wrote the script below to use as a proxy between Flash and Flickr. You will need to use a proxy of some kind anyway as you cannot connect directly from a Flash movie on one domain to servers on another domain (due to the cross domain security sandbox in the Flash Player). This proxy saves all the XML it receives from Flickr as files on your file system. Then if it can’t reach the Flickr servers (e.g. when you are offline) it will serve up the contents of these files instead. Well – it will if you make a call to the API which is identical to one which has been cached. Otherwise it will serve up a custom error document which is similar to the standard Flickr error document except it contains a unique error code so you can deal with it in your application.

Without further ado, here is the code:

define("_SAVE_PATH", dirname(__FILE__)."/downloaded/");
define("_ERROR_MESSAGE", "< ?xml version=\"1.0\" encoding=\"utf-8\" ?><rsp stat=\"fail\"><err code=\"999\" msg=\"No access to and no cached result for this request\" /></rsp>");
define("REST_ENDPOINT", "");

if ($fp = @fopen("", "r")) {
    // we are online and can see - cache any requests that come in
    define("_ONLINE", 1);
} else {
    // we can't see - 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);

$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);
} 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;

As you can see, it’s pretty simple really. It’s not tested all that comprehensively yet but it should work. The next thing to do is to write a script which automatically calls this script once for each possible outcome of each call to the Flickr API so that you have a complete library of the possible returns on your machine.

One side note… I wouldn’t recommend running this script on a public webserver. It doesn’t feel too secure to me to be writing files which are named based on the querystring to your webserver. At least change the _SAVE_PATH so that people don’t know where to look for the files. Probably a bit paranoid but better safe than sorry, ey? And anyway – you should be running the it on your laptop anyway so this shouldn’t be an issue :)

If you want to download the complete script with commenting etc then please do: flickr_cache.phps. Any comments, ways to improve it or ideas on how to easily implement the “call every method once” script please use the form below…

3 Comments, Comment or Ping

Reply to “Simulating access to the Flickr API offline”