Parsing Flickr Feed with PHP tutorial

I’ve spent about 30 minutes to find a javascript embed to print out a custom thumbs list of flickr photos,…

Maggio 30, 2010

I’ve spent about 30 minutes to find a javascript embed to print out a custom thumbs list of flickr photos, but I didn’t find anything clean enaugh… and I’ve not enaugh time to spend to read the flickr’s API’s…
So, I’ve searched for the feed of the user, and I’ve found it at the bottom of flickr’s pages: I’ve decided to grab the feed and parse it to get my custom gallery.
If you’re looking for the flickr’s feed it’s here, at the bottom:

flickr feed

If you click on the feed link you will open the feed (this can be shown in different ways depending on your browser). If you look the URL of the feed you’ve clicked, you can see that it contains the “id” of the feed on flickr database, here is the id:

feed id

Take the id. And then use this php function to grab and create the thumbs list, this code will simply output anchors and images, so you have to use css to customize it as you want:

function attr($s,$attrname) { // return html attribute
	preg_match_all('#\s*('.$attrname.')\s*=\s*["|\']([^"\']*)["|\']\s*#i', $s, $x);
	if (count($x)>=3) return $x[2][0]; else return "";
}

// id = id of the feed
// n = number of thumbs
function parseFlickrFeed($id,$n) {
	$url = "http://api.flickr.com/services/feeds/photos_public.gne?id={$id}&lang=it-it&format=rss_200";
	$s = file_get_contents($url);
	preg_match_all('#<item>(.*)</item>#Us', $s, $items);
	$out = "";
	for($i=0;$i<count($items[1]);$i++) {
		if($i>=$n) return $out;
		$item = $items[1][$i];
		preg_match_all('#<link>(.*)</link>#Us', $item, $temp);
		$link = $temp[1][0];
		preg_match_all('#<title>(.*)</title>#Us', $item, $temp);
		$title = $temp[1][0];
		preg_match_all('#<media:thumbnail([^>]*)>#Us', $item, $temp);
		$thumb = attr($temp[0][0],"url");
		$out.="<a href='$link' target='_blank' title=\"".str_replace('"','',$title)."\"><img src='$thumb'/></a>";
	}
	return $out;
}

// usage example:
echo parseFlickrFeed("16664181@N00",9);
// you have to use css to customize it

Like here:
flickr thumbs css

This code will be addedd to the next version of Mini Bots Class.

Author

PHP expert. Wordpress plugin and theme developer. Father, Maker, Arduino and ESP8266 enthusiast.

Comments on “Parsing Flickr Feed with PHP tutorial”

There are 4 thoughts

  1. kn00tcn ha detto:

    perfect, all i needed was some thumb links, no fancy api or anything & of course vanilla code so i can add my own css

    could even cache the output if you wanted to

  2. Viktor Hanacek ha detto:

    Great! Thank you for your awesome work! I dislike plugins, I like pure PHP code and this post really helped me. Thank you.

  3. H. ha detto:

    Great script, thank you. How can I get a larger image size? I want to get medium (the one with _m. suffix)

  4. H. ha detto:

    Nevermind, I got it :) Just replaced media:thumbnail with media:content

Comments are closed