Return to Snippet

Revision: 47936
at June 18, 2011 20:21 by jakekimpton


Initial Code
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 "";
}


function getFlickrFeed($id,$n) {

	$urlenc = urlencode("http://api.flickr.com/services/feeds/photos_public.gne?id=$id&lang=en-us&format=rss_200");
	$url = "http://pipes.yahoo.com/pipes/pipe.run?_id=HHvHTP7h2xGPZ1tDdbq02Q&_render=rss&urlinput2=$urlenc";
	$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");
		
		$thumb2 = explode("_",$thumb);
		$img =  $thumb2[0]."_".$thumb2[1].".jpg";
		//print $img;
		$out.="<a href='$link' target='_blank' title=\"".str_replace('"','',$title)."\"><img src='$img'/></a>";
	}
	return $out;
}

// Example Request with ID and how mant photos required
echo getFlickrFeed("[email protected]",9);

Initial URL
http://www.webchase.co.uk/blog/462/flickr-recent-photos-to-html-with-php

Initial Description
It’s been driving me crazy that there’s no definitive script written to take out a user’s Flickr recent photos and parse it for formatting into HTML with a larger size than the standard 75 x 75 dimensions that the public_photo.gne Flickr script outputs. So I’ve written one, using a bit of skill and a snippet of code that someone posted which was kind of going in the same direction but then suddenly stopped and took a hard u-turn, veering off to somewhere stupid, as usual with programmers who know too much. So here’s a quick run through of what the code does:

Collect the User ID that is added manually and constructs the Feed Links
Get the File contents of the Feed Link once it’s been run
Convert the XML to HTML
Return it to the script for printing or a variable (Choice of the Programmer)

I haven’t tested this on different versions of PHP, all I know is it works for me, and it’s using some pretty simple stuff so it should work fine. Just change the ID to your own or whoever’s it has to be and it should run.

Initial Title
Flickr Recent Photos to HTML with PHP

Initial Tags
php, html, xml

Initial Language
PHP