Using the Flickr API

Mark Birbeck's picture

Flickr provides a number of different services for interacting with its data. If you look at the list of methods that the Flickr API supports, you'll see they are divided into groups; one group of API methods deals with photos, another with people, and so on.

Flickr can accept requests to these methods in a number of different formats, all of which we can use with XForms. The easiest to understand and test is REST, and to use it to make a call to any one of the methods listed we need to go through the following URI:

http://www.flickr.com/services/rest/

The parameters that you must pass are the method you want to call, and a special application key. The key must be obtained from Flickr, and although we have one for this tutorial, if you plan to develop the sample form further you should get your own key.

The particular method we will use for this form is flickr.photos.search, and you can see from the documentation that this method takes a number of parameters. Of the optional parameters, the only ones that we'll use are tags and per_page.

The tags parameter contains the tag or tags that the user of our form wants to search for, whilst per_page indicates how many items 'per page' we would like to be returned; since there may be tens of thousands of items that match a particular tag, Flickr will give the list to us one page at a time, and this parameter indicates how many items we would like on each of those pages.

Combining these two method-specific parameters with our general ones, and using the 'REST URL', we have a resulting call to the method that looks like this (the extra line breaks are to make it easier to read):

http://www.flickr.com/services/rest/?
  method=flickr.photos.search&
  api_key=68149024a667e0be3c63708f002ffe1e&
  tags=dogs&
  per_page=12

The results of such a call might be something like this (try it):

<rsp stat="ok">
  <photos page="1" pages="983" perpage="24" total="98265">
    <photo id="94048829" owner="54228211@N00" secret="9b2e52936b"
     server="37" title="These paws are made for walking - 5997"
     ispublic="1" isfriend="0" isfamily="0"
    />
    <photo id="94044468" owner="23819962@N00" secret="1641bc41c5"
     server="41" title="listen up dogs"
     ispublic="1" isfriend="0" isfamily="0"
    />
    .
    .
    .
  </photos>
</rsp>

All data from Flickr is structured in much the same way as this; the containing element is always rsp, and there is always a stat attribute that indicates whether the request was successful or not.

What is inside the rsp element will change from method to method, but if the result is a set of photos, then they will always be in a list like this. Note that there is actually no URL for the image itself in the set of results, since it is our job to create it by combining together the information passed to us in the photo element. This is because Flickr stores multiple formats of the same image, and we'll see how to combine the pieces later.