Flickr and Zend_Rest_Flickr - Zend has become one of the leading personal image storage websites. Take a look at it by loading up the web site You can search for specific snapshots taken by users just like you and me, upload your own photographs, group your photographs, add cool excerpts to the photo, and even purchase a snapshot using one of their third-party applications.

Zend Framework utilizes the Zend_HTTP and Zend_Rest components to provide the easy access to Flickr that the Zend_Service_Flickr gives you.

So what can you do with the Zend_Service_Flickr component?

  • Retrieve photos based on specific tags/keywords.
  • Retrieve photos for a specific users.
  • Retrieve photos for a specific group ID.
  • Return image details.
  • Move seamlessly through result sets using the SeekableIterator.

Getting Started with the Flickr Service

To get you started using Zend_Service_Flickr features, you need a developer account, which allows to generate your very own application ID and a secret key. You need only the application ID in the following examples. Open the URL and click the link “Apply for a new API key.” Once you have successfully created a uique key, you’re set.

Connecting to the Flickr Web Service API

The first test will be to connect to the web service API. You will create a simple connection and return a user’s photos based on the e-mail address.

You create a new test action, testFlickrConnAction(), in the TestWSController.php file. The action will use the Zend_Service_Flickr component to connect to the Flickr web services and then search for all photos available for a specific e-mail address. The code in Listing automatically loads the required file Zend/Service/Flickr.php. The file contains all the operations that allow you to instantiate and use methods for searching.

Listing Retrieving Photos by E-mail

After you load the required file, you create a Zend_Service_Flickr object: $flickr. The constr-uctor requires the application ID Flickr generated for you before you can successfully instantiate the Zend_Service_Flickr object. You use the userSearch() method, which allows you to fetch photos for a specific user by passing in a string e-mail address into it. Finally you store the result set into a class view variable:$photos.

Because the results are stored inside the $photos variable as a Zend_ Service_ Flickr_ Result Set object, you can then use the view variable in a new view: test-flickr-conn.phtml, as shown in Listing.

Listing Flickr Test View test-flickr-conn.phtml

Copy the markup into a new file and place it into the application/views/scripts/testws folder and then load the URL.

Search Using Tags

Searching for user-based content, as Listing demonstrates, is one way of locating photos if you know which user has the best pictures for your subject. But what if you don’t know which user has the photo you want? You could try to look through each user’s profile, but with Flickr containing millions of users, and each user owning up to 100 or more photos, you can spend your whole life searching for the right photo. There’s a better way: search for photos using a tag-based approach:

tagSearch(string, array)

The tagSearch() method accepts two parameters: the initial parameter contains a keyword the photo must contain. The second optional parameter is a key-value array of options.

The code demonstrated in Listing “Retrieving Photos by E-mail” tarts like Listing, but instead of the userSearch()method, tagSearch() is used.

Listing tagSearch() Implementation

Pass the php and zend tags into the tagSearch() method. You then store the result set for later use in the view. By default, the result set contains 10 photos; with the second parameter you can overwrite this option and set it to 20 by using one of the options found in Table.

Table Flickr Options

Option Description

Using the same view markup you used in Listing, create a new view, test-flickr-tags.phtml, and save into the application/views/scripts/testws directory. Now load the URL and you should see a similar result set as shown below.

Tag-based search result

Search for User

The Zend_Service_Flickr component contains three methods that allow developers to search for user photos based on an e-mail address and to retrieve the ID of the user using the e-mail address or username.

The userSearch() method accepts two parameters: the initial parameter is the full e-mail address of the user you want to retrieve the photos from; the second optional parameter should contain the options located in Table.

The remaining two methods allow you to return the ID of the user based on an e-mail address and username. getIdByEmail() and getIdByUsername() both accept a single string parameter and return a string containg the ID of the user, if found. Calling the method can be done like so:


The code returns the ID of the user with the specified e-mail address (if it exists).

Search for Group Photos

Flickr offers a wide variety of photo support and user participation. Users can comment on each other’s photos, so it comes as no suprise that Flickr has allowed users to join together into groups that share the same interests. If you’re a fan of crazy costumed fans at KISS concerts, you can find others who share their recent Gene Simmons and gang photos on Flickr. Let’s see how you can view it by using the Zend_Service_Flickr component.

You need the group ID, which you find by doing a search for a group. Head to the Flickr site and do a quick search for Kiss Army. If nothing comes up, don’t worry; click the Groups tab on top of the page. You should now see the KISS ARMY group. Give it a click and watch the photos come up. Great;

One is the kissarmy URL; the other is the Pedro Infante group, which does not have a resolved group ID; it has a numerical representation of the group ID. You can use either as the group ID. Listing shows how to get the group photos.

Listing GroupPoolGetPhotos Example


The result set of a call into Flickr returns not only a collection of photos but also provides you with a Zend_Service_Flickr_ResultSet object. The object contains basic functionality.the capability to loop through a collection of photos using a foreach() function and discover the total number of photos returned.


The Zend_Service_Flickr_ResultSet object contains a collection of Zend_ Service_ Flickr_ Result object(s) if there are photos found. The object contains valuable information that you want and is the meat and potatoes of what you’re after.

Table contains a full list of information you can retrieve from inside the object.


Table a Zend_Flickr Result Attributes

LoudBite: Artist Flickr Photo Stream

You’ll continue expanding the application by adding a gallery to each of the artist pages. The beauty of it is that you have to create only one set of PHP files that will add the gallery to the site. The module will need two pieces; the initial piece is the controller that has already been created: ArtistController.php. The second item is the view that contains the markup to render the located photos.

The first piece of the module is the controller. Create a new action, listPhotosAction(), to handle the retrieval of artist photos and will place the photos into the view. It will also check whether the user has supplied the artist name. If not, the user is taken to an error page.

The action shown in Listing demonstrates the behavior you need. When the user loads the URL, the action will check whether the artist name has been passed into the action as a parameter using the request object’s getParam() method. If the action detects the artist name, continue on to the Flickr call.

Listing listPhotosAction()

You create an instance of the Zend_Service_Flickr object, $flickr, pass in the developer key, and call the tagSearch() REST web service method. By using the tagSearch() method, you fetch all the photos that have been tagged with the artist’s name and expect a Zend_Service_Flickr_ResultSet object returned. Finally, you pass the object into the view. Listing shows the list-photos.phtml file that you should have in application views/scripts/artist.

Listing list-photos.phtml

The foundation of the view can be taken from any of the other examples. Take a look at Listing; you can use the same markup as list-photos.phtml. You want it compact enough to fit anywhere but also able to display enough photos so that the module makes sense to have. You start off by seeing whether the result set, $this->photos, contains any photos. This allows you to display a friendly message to the user if there are no photos. If there are photos, go ahead and create the layout. Create a foreach loop that loops though the result set in the middle of the layout to retrieve the Zend_ Service_ Flickr_ Result and then Zend_ Service_ Flickr_ Image objects. You primarily use the Small portions of the result object and the image location for display. You also create a hyperlink that will link the thumbnail in the module to the full image on Flickr.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

Zend Topics