Building a blog with Flickr

This looks like a really old post (I mean, it's from 2008 for goodness sake). Stay for the retro vibes but be aware any information in this post is likely way out of date.

About a year ago I became the proud owner of a 1979 MGB GT and began restoring it. I’d been thinking about setting up a blog about the process for a while and have finally got around to it. Luckily, over the past few months I’d been pretty good at taking photographs to document it all and had uploaded them to Flickr.

Inca Yellow screenshot

Now, I’ve used Expression Engine before and this was my first thought when I sat down to put my MG blog together. However it did seem a bit of overkill for what I wanted and I’d been there, done that and wanted to try something a bit different.

As I’d been uploading all my MG photos to Flickr and knowing that their photo description field allows a limited but totally sufficient set of html tags coupled with a nice API (even though it’s not RESTful), I decided to try using Flickr as a blog engine.

Before I go on, take a quick look at the result: IncaYellow.com. [Editors note from 2023 - the site is no longer running on the Flickr API.]

The Flickr API is really pretty nice to work with. However, it can be a touch slow so I added in a bit of server-side caching to save visitors having to wait too long. For example, this is pulling in the individual photo information with a little bit of simple caching:

function getPhotoInfo($p) {
// build the API URL to call
$params = array(
    'api_key' => 'YOUR_API_KEY',
    'method' => 'flickr.photos.getInfo',
    'photo_id' => $p,
);
$encoded_params = array();
foreach ($params as $k => $v){
    $encoded_params[] = urlencode($k).'='.urlencode($v);
}

//call the API and decode the response
$flickrurl = "http://api.flickr.com/services/rest/?".implode('&', $encoded_params);

//set cache options
$cachefile = 'PATH_TO_CACHE_FOLDER'.$p.'photoData.xml';
$cachetimelimit = ((60 * 60) * 24); //day


//use the cache if newer than $cachetimelimit
if (file_exists($cachefile) && time() - $cachetimelimit < filemtime($cachefile)) {
    $xml = file_get_contents($cachefile, true);
} else {
    //get the data and save to the cache
    $xml = file_get_contents($flickrurl);

    // Cache the output to a file
    $fp = fopen($cachefile, 'w');
        fwrite($fp, $xml);
        fclose($fp);
    }

    //dump the xml in a variable
    if ( $xml ) {
        $theStuff = simplexml_load_string($xml);
    }
    return $theStuff;
}

You can then parse the resulting xml file to pull out the required info. Using a couple of other Flickr API methods and the Delicious API meant I could pretty much reproduce a full-on blog, complete with archives and tag pages.

Now obviously this wouldn’t suit most blogs, but I knew that my posts would be pretty short and always be accompanied by a photo, so I was set.

There are a few enhancements I’ve got in mind for the next few weeks. The main one is being able to add more than one photo for a given post, especially for those more tricky mechanical jobs. This seems like a perfect opportunity to use machine tags to relate photos to each other and by adding something like:

incayellow:post:PHOTOID

to a photo, I could pull in all the other photos to do with that job. Comments are another nice-to-have, so I’ll be building that in too. I’ll also be tweaking the caching timing over time to find the optimal period to keep data for (it’d be nice to have the cache refresh for new posts). The other thing I just haven’t got around to yet is an RSS feed. I could just use the Flickr RSS feed, but that just seems lazy, so I’ll most likely roll my own. Also on the horizon is a 404 page, just because I think you should always have one.