Building Flickr URLs from YQL flickr.photos.search results

Working with the Flickr photo search table within the Yahoo! Query Language (YQL) can make it hard for developers to display the photos. The image URLs have to be put together from different parts returned from the API, such as owner, server, farm, secret, and others. Furthermore, you need to link the images back to the author pages. Here you'll learn how that can be done easily.

When we use YQL, the structure of results returned by a Flickr photo search contains all the pieces that you will need to generate a whole series of author and photo links with Flickr, but it does not go the extra step of returning these links back to you. For instance, if we run a Flickr photo search query within YQL, the result set that is returned back from YQL from flickr.photos.search looks something like this:

Now let's take a look at how to do something with those data pieces.

Generating an image source

Using the farm, server, id, and secret parameters from the result set, we can generate an image source that will display the searched image. The structure of the source using these parameters will look like this:

<img src="http://farm{$farm}.static.flickr.com/{$server}/{$id}_{$secret}.jpg" alt="{$title}" />


Generating a link back to the photo page on Flickr

Under the terms of service, all photos that you use from Flickr need to link back to the Flickr photo page of the owner. Using the owner and id parameters from the result set, we can generate a link back to the Flickr page for the searched image. Building upon the image source that we built above, we can wrap the image with a link that sends the user back to the original image page on Flickr:

<a href="http://www.flickr.com/photos/{$owner}/{$id}">
<img src="http://farm{$farm}.static.flickr.com/{$server}/{$id}_{$secret}.jpg" alt="{$title}" />
</a>


Generating a link back to the photo owner's profile

It's always nice to provide a credit for the photo by linking back to the root profile of the photo owner (instead of just the photo page that we saw above). By using the owner variable in the photo return data, we can easily create a link back to the profile of the photo owner and give him or her credit. Building upon our current linked photo example, we can add in one more piece to do this:

<a href="http://www.flickr.com/photos/{$owner}/{$id}">
<img src="http://farm{$farm}.static.flickr.com/{$server}/{$id}_{$secret}.jpg" alt="{$title}" />
</a>
<a href="http://www.flickr.com/photos/{$owner}">Photo Owner</a>

Now let's see how to code a script in PHP that will output the photos wrapped in links back to their original sources (as seen in the second example above, listed as "Generating a link back to the photo page on Flickr"). What we do in the following code is generate the YQL query URL, capture the results into a variable using simplexmlloadfile, then pass the array of photos through to a function to build the HTML for us.

These are easy ways to display Flickr photos and links based on the results returned from the flickr.photo.search table in YQL. They're simple examples but worth documenting so that you don't need to go through the hassle of piecing the URL structures together on your next project.

Jonathan LeBlanc (@jcleblanc) Technology Evangelist, Yahoo! Developer Network

Jonathan LeBlanc () Jonathan LeBlanc works with the Yahoo! Developer Network as a principal software engineer / technology evangelist. Focusing on partner relationships and training, as well as external developer integrations, Jonathan works with and promotes emerging technologies to aid in the adoption and utilization of new social development techniques. As a software engineer, Jonathan works extensively with social interaction development on the web, developing new methods for linking social networks to drive the ideal of an open web.



Post a Comment

You must be logged in to Yahoo! to comment. .