Introduction to Flickrs API

Outside of Google Maps, I think Flickr has to be my favorite web service. If you can boil any content down to a word or a phrase, you can usually get a hit on it on Flickr. This adds a new dimension to just about any application. Note, the pictures you get from Flickr are usually not directly related to your application and sometimes, they can be "not safe for work", so be careful when playing around with Flickr.

Before we dive into the code, let's talk a bit about web services and Zend Framework. Web services are basically any data or functionality that is exposed via a web interface. If you are new to web services, I recommend you check out for a taste ofwhat is available. Web services are not always free like the ones we are using. Some companies charge for access to their data and services, as is their right. Make sure, before you start building the next great mashup, that you know the services you are dealing with.

Web services come in several flavors; XML-RPC, SOAP, REST, and of course, CUSTOM. (Custom is usually those people who didn't feel like adhering to an existing standard so they just hacked something together and insist that it's better than anything else out there.) For our demonstration, we will be using REST interfaces because they are by far the simplest. (REST stands for Representational State Transfer and there's a good article on Wikipedia about it if you want more info. REST can be, if you want it to, complex. However, for our purposes, we can define REST as "we call a URL, it returns to us XML". You can already see that in the call to the Yahoo! API we are using.

Zend Framework has a growing collection of APIs that are supported with classes in the framework; one of them is Flickr. You have already seen Zend_Rest_Client at work when we wrote our IndexController. You know how easy it is to call a simple web service and process the results. In many cases though, the APIs are not as simple as Yahoo's Term Extraction API. Many times there is authentication to deal with and a myriad of methods to figure out. As you can see from, Flickr has a lot of options for us to choose from. Lucky for us though, someone has already done most of the work for us and created Zend_Service_Flickr.

We don't really get a feel for it with this example, but one of the things I love about the way that Zend Framework handles APIs is that all properties become properties of the class and any action you can take using the API becomes a method. The simple example we use with Flickr is tagSearch(). Normally, to actually do a search using the Flickr API you would build a valid URL appending each property/value pair to build the query before executing it. Zend Framework's Zend_Rest_Client handles all of this for us transparently. We can use simple properties to build the query and call a method of the object to actually make the call and fetch the results.

In our case, as you will see, we instantiate an instance of Zend_Service_Flickr, passing in our API key as the constructor parameter.

A word here about API keys - get your own. You can undoubtedly use the one that I use in the sample; however, all that will do is get it canceled. In this case, it's no big deal because I registered it just for the book. However, Flickr hands them out for free and makes them very easy to get, so get your own. Visit for more information.

Once created, we can simply call tagSearch(), passing in an array of parameters that we want passed on to Flickr and what we get back is a Zend_Service_Flickr_ResultSet, a beautifully object oriented representation of the XML that Flickr handed back.

Was this article helpful?

0 0

Post a comment