Sure, Flickr, has over 5 billion photos with all sorts of meta information (like location or subject of the photo) connected to them. Wouldn't it be nice to access those photo's and data within your own application? This is were connecting to the Flickr API comes in handy. So nice of the folks at Flickr to provide a REST Webservice for just doing that.

REST (Representational state transfer) is a way to create, read, update or delete information on a server using a HTTP request (a more complex alternative would be SOAP). The response from Flickr will be in XML (other REST response formats would be JSON, HTML or CSV). At Flickr there are currently about 180 HTTP callable methods. They are all listed under 'API Methods' at the far right of the Flickr API page.

  • Let's start with getting an API key, by clicking the 'Creat an App' link at the Flickr API page.
  • Check if your key is valid by calling the method flickr.test.echo with the URL https://api.flickr.com/services/rest/?method=flickr.test.echo&api_key=your_key_here. The response should be:
  • The method to search for a photo on subject or tag is flickr.photos.search. The URL for searching photos with the tag 'sunset' with a limit of 3 results would be https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=your_key_here&tags=sunset&per_page=3. The current (when you make your own request there will be a new result set due to added photographs with the tag 'sunset') response from Flickr:
  • Since the response is in XML we don't see the actual photo yet. However, if you look closely at the response there is a photo id and owner stated: for the first photo node id="14577962925" and owner="125902894@N06". With those values the photo URL at Flickr can be deducted since a photo URL consists of https://www.flickr.com/photos/owner_value/id_value. So, let's visit https://www.flickr.com/photos/125902894@N06/14577962925. You should see the webpage with the picture now: flickr-rest-how-to-Sunset-1

  • In our application we want to show pictures i.e. photographs not webpages. If we examine the above webpage we see the actual image file is stored at https://farm6.staticflickr.com/5507/14577962925_c6ed60db6a.jpg (with a smaller version at https://farm6.staticflickr.com/5507/14577962925_c6ed60db6a_n.jpg and a bigger one at https://farm6.staticflickr.com/5507/14577962925_c6ed60db6a_n.jpg).

  • If we examine the URL https://farm6.staticflickr.com/5507/14577962925_c6ed60db6a.jpg we recognize the farm attribute farm="6", server attribute server="5507", id attribute id="14577962925" and secret attribute secret="c6ed60db6a" we've seen earlier in the Flickr XML response.

  • How to transform the XML response from Flickr to actual images being displayed? Here's were the PHP function simplexml_load_file() comes to the rescue. This function interprets an XML file into an object which makes it ease to iterate over each photo node.

  • Let's write the class with the function simplexml_load_file() included:

  • The only thing left to do is to make the form to pass the requested data to the class:
  • Let's see the result of our work; try the form below!


  • My custom Flickr photo search app






comments powered by Disqus