After having to build out the URL structure from the Yahoo! Query Language (YQL) results returned by the flickr.photos.search table yet again, I’ve decided to actually document how to generate Flickr URLs using the results returned from YQL.
When we use YQL the structure of results returned by a Flickr photo search contains all of 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 the YQL (e.g. http://developer.yahoo.com/yql/console/?q=select%20*%20from%20flickr.photos.search%20where%20has_geo%3D%22true%22%20and%20text%3D%22san%20francisco%22) the result set that is returned back from YQL from flickr.photos.search looks something like this:
true
445
444
5275
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:
<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 will 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}' target='_blank'>
<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/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}' target='_blank'>
<img src='http://farm{$farm}.static.flickr.com/{$server}/{$id}_{$secret}.jpg' alt='{$title}' />
</a>
<a href="http://www.flickr.com/photos/{$owner}" target="_blank">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 code below is generate the YQL query URL, capture the results into a variable using simplexml_load_file, then pass the array of photos through to a function to build the HTML for us.
<?php
$yql_url = 'http://query.yahooapis.com/v1/public/yql?';
$query = 'SELECT * FROM flickr.photos.search WHERE has_geo="true" AND text="san francisco"';
$query_url = $yql_url . 'q=' . urlencode($query) . '&format=xml';
$photos = simplexml_load_file($query_url);
$result = build_photos($photos->results->photo);
echo $result;
function build_photos($photos){
$html = '';
if (count($photos) > 0){
foreach ($photos as $photo){
$html .= "<a href='http://www.flickr.com/photos/{$photo['owner']}/{$photo['id']}' target='_blank'><img src='http://farm4.static.flickr.com/{$photo['server']}/{$photo['id']}_{$photo['secret']}.jpg' width='75' height='75' alt='{$photo['title']}' /></a>";
}
} else {
$html .= 'No Photos Found';
}
return $html;
}
?>
These are easy ways of displaying 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 pieces the URL structures together on your next project.
- Jonathan LeBlanc
2 Comments | In: YQL | tags: Flickr, PHP, YQL. | #