Getting a list of Flickr photos by location and/or search term with a YQL open tableMonday, November 2nd, 2009 at 11:44 am
Displaying photos from Flickr can be daunting. The API needs authentication and the RSS, JSON or LOLcode output is very limited. The way around is using YQL and its Flickr tables. That way it is pretty easy to search Flickr:
select * from flickr.photos.search where text=”panda”
The output format has a lot of information in there, but sadly enough, not all. For example the real name of the owner or the description is missing. Therefore you need to go through yet another Flickr API to get the whole data set:
select * from flickr.photos.info where photo_id in(
select id from flickr.photos.search where text=”panda”
Guess what? You can also try this more detailed query in the console.
I’ve shown before how easy it is to display Flickr Photos retrieved that way:
The main issue is that Flickr returns the photos in a pretty weird format and that you need a script like the one above to turn it into a simple HTML list.
You’ll notice that while the E4X support is very powerful, it can be a bit confusing to look at on first sight. Once you got your head around though it becomes much cleaner that way.
You can use this table like any other open table via the
use command in YQL:
use “http://github.com/codepo8/yql-tables/raw/master/flickr/flickr.photolist.xml” as flickr;
select * from flickr where text=”me” and location=”uk” and amount=20
I’ve wrapped one more API in there – the Yahoo Geo API to determine a place from a name should you want to search by location. All in all you have three parameters in this open table – all of which are optional:
- text – the search text
- location – the geographical location
- amount – the amount of photos to be returned
If you look at the table source, you can also see that I hard-wired the license of the photos to 4 which is CC-BY. So if you link the photos back to Flickr you both satisfied Flickr’s terms and the original photographer’s.
Now, the easiest way to use this output is by using YQL’s JSON-P-X output format. This is XML with a callback which returns a JSON object with the HTML as a string instead of a convoluted JSON object. See the JSON-P-X output here.
And also in PHP:
$url = ‘http://query.yahooapis.com/v1/public/yql?q=use%20%22http://github.com/codepo8/yql-tables/raw/master/flickr/flickr.photolist.xml%22%20as%20flickr;%20select%20*%20from%20flickr%20where%20text=%22me%22%20and%20location=%22uk%22%20and%20amount=20&format=xml&diagnostics=false’;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
$output = preg_replace(‘/.*
$output = preg_replace(‘/
$output = preg_replace(‘/.*?>/’,’‘,$output);
$output = preg_replace(‘//’,’‘,$output);
You can see both in action on the demo page.