RSS Feeds for Photos With Multiple Tags in Flickr

There is an updated version of my script available!. Thanks Timtom!

In its RSS feeds, Flickr doesn't let you get photos tagged with all tags you tell it in an RSS feed, so I whipped up a script that, if you have your own server, creates RSS feeds of the tags you specify. If you called it 'multitags.php', and you put it on your site, say example.com, the URL would look something like http://www.example.com/multitags.php?tags=vancouver+seawall That would get you an RSS feed (in a format very similar to the format of the RSS feeds that Flickr outputs) of photos that are tagged with both 'vancouver' and 'seawall'.

If you used a comma instead of a plus, it looks for photos tagged with either of the tags, so http://www.example.com/multitags.php?tags=vancouver,seawall would get you photos tagged with 'vancouver' mixed in with the photos tagged with 'seawall'. There is a way to get RSS feeds of tags this way, but why figure that out. How to do it is in a forum somewhere, but I couldn't remember where, so I just coded that into the script.

You'll need two things to use the script: the excellent phpFlickr library (support for uploading coming, nice!) and a Flickr API key, which you can get at http://www.flickr.com/services/api/. You'll need to change the three variables at the top of the script, which follows, accordingly.

A note about the code: the first little bit about the XML declaration making up multiple lines is a workaround for the code dispaly. And yes, there are a lot of print statements. It makes it easier for me to debug.

It's probably best to keep the URL secret, so that the only person hitting the script is you. The RSS feeds work for me in NetNewsWire, but please do submit bug reports in the comments.

$php_flickr_cache = '/lib/phpflickr/cache';
$flickr_api_key = 'your Flickr API key here';
require_once($php_flickr_location . '/phpFlickr.php');
$f = new phpFlickr($flickr_api_key);
if (isset($php_flickr_cache)) {
$f-?>enableCache("fs", $php_flickr_cache);
}
if ($_GET && $_GET['tags']) {
$tags = urlencode($_GET['tags']);
$tag_mode = 'any';
if (strstr($tags, '+')) {
$tags = str_replace('+', ',', $tags);
$tag_mode = 'all';
}
else if (strstr($tags, "%2C")) {
$tags = str_replace('%2C', ',', $tags);
$tag_mode = 'any';
}
}
$photos = $f->photos_search(array('tags' => $tags, 'tag_mode' => $tag_mode, 'per_page' => '15'));
header("Content-type: text/xml");
$rss_feed = " $rss_feed .= "?>\n";
$rss_feed .= "\n";
$rss_feed .= " \n";
$rss_feed .= " Flickr Photos tagged with ";<br /> $rss_feed .= str_replace(",", ' and ', $tags);<br /> $rss_feed .= "\n";
$rss_feed .= " Script by Richard Eriksson http://justagwailo.com/\n";
$requested_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$rss_feed .= "
$requested_url\n";
$rss_feed .= "
\n";
foreach (
$photos['photo'] as $id) {
$u = $f->urls_getUserPhotos($id['owner']);
$rss_feed .= " \n";
$link = $u . $id['id'] . "/";
$rss_feed .= "
$link\n";
$rss_feed .= " $link\n";
$rss_feed .= " ";
$username_elements = explode("/", $u);
$username = $username_elements[4];
$rss_feed .= "<p><a href=\"$u\">$username</a> has posted a photo:</p>\n\n";
$rss_feed .= "<p><a href=\"$link\">";
$rss_feed .= "<img src=\"http://farm";
$rss_feed .= $id['farm'];
$rss_feed .= ".static.flickr.com/";
$rss_feed .= $id['server'];
$rss_feed .= '/';
$rss_feed .= $id['id'];
$rss_feed .= "_";
$rss_feed .= $id['secret'];
$rss_feed .= "_m.jpg";
$rss_feed .= "\" alt=\"". $id['title'] . "\" style=\"border: 1px solid #000000;\"/></a></p>";
$rss_feed .= "
\n";
$rss_feed .= "";<br /> $rss_feed .= $id['title'];<br /> $rss_feed .= "\n";
$rss_feed .= "
\n";
}
$rss_feed .= "
\n";
$rss_feed .= "
";
print $rss_feed;
?>

There is an updated version of my script available!. Thanks Timtom!

In its RSS feeds, Flickr doesn't let you get photos tagged with all tags you tell it in an RSS feed, so I whipped up a script that, if you have your own server, creates RSS feeds of the tags you specify. If you called it 'multitags.php', and you put it on your site, say example.com, the URL would look something like http://www.example.com/multitags.php?tags=vancouver+seawall That would get you an RSS feed (in a format very similar to the format of the RSS feeds that Flickr outputs) of photos that are tagged with both 'vancouver' and 'seawall'.

If you used a comma instead of a plus, it looks for photos tagged with either of the tags, so http://www.example.com/multitags.php?tags=vancouver,seawall would get you photos tagged with 'vancouver' mixed in with the photos tagged with 'seawall'. There is a way to get RSS feeds of tags this way, but why figure that out. How to do it is in a forum somewhere, but I couldn't remember where, so I just coded that into the script.

You'll need two things to use the script: the excellent phpFlickr library (support for uploading coming, nice!) and a Flickr API key, which you can get at http://www.flickr.com/services/api/. You'll need to change the three variables at the top of the script, which follows, accordingly.

Comments

I am getting a header issue. I get the following. Can you help? Warning: Cannot modify header information - headers already sent by (output started at /home/husarus/public_html/phpFlickr/flickr_multitag.php:4) in /home/husarus/public_html/phpFlickr/flickr_multitag.php on line 30 After that I do get the full feed just fine. Cheers.

Ed and I talked in IM, and we figured it out: it was whitespace at the top of the script.

That's a great work :-) Thank you for the script

Well I get some results, but NOT the ones I am expecting. I have 43 photo's in a set on Flickr account and they are all tagged Llangollen Holiday 2007 as a result of the set name. If I try ?tag=Llangollen+Holiday+2007 I do not get my photo's in the feed. Also tried ?tag="Llangollen Holiday 2007" but no joy

Looks like the script could use some dedicated attention, since it's possible the script (written 2005) might be out of date with regards to the Flickr API. I'll take a look at overhauling it, if that's what it needs.

Would it be possible to incorporate this multiple tags script with the wordpress plugin flickrRSS? With limited knowledge of php, I cannot figure how. Tks.