Flickr Recent Photos to HTML with PHP

/ Published in: PHP
Save to your folder(s)

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.

Copy this code and paste it in your HTML
  1. function attr($s,$attrname) {
  2. // return html attribute
  3. preg_match_all('#\s*('.$attrname.')\s*=\s*["|\']([^"\']*)["|\']\s*#i', $s, $x);
  4. if (count($x)>=3) return $x[2][0]; else return "";
  5. }
  8. function getFlickrFeed($id,$n) {
  10. $urlenc = urlencode("$id&lang=en-us&format=rss_200");
  11. $url = "$urlenc";
  12. $s = file_get_contents($url);
  13. preg_match_all('#<item>(.*)</item>#Us', $s, $items);
  14. $out = "";
  15. for($i=0;$i<count($items[1]);$i++) {
  16. if($i>=$n) return $out;
  17. $item = $items[1][$i];
  18. preg_match_all('#<link>(.*)</link>#Us', $item, $temp);
  19. $link = $temp[1][0];
  20. preg_match_all('#<title>(.*)</title>#Us', $item, $temp);
  21. $title = $temp[1][0];
  22. preg_match_all('#<media:thumbnail([^>]*)>#Us', $item, $temp);
  23. $thumb = attr($temp[0][0],"url");
  25. $thumb2 = explode("_",$thumb);
  26. $img = $thumb2[0]."_".$thumb2[1].".jpg";
  27. //print $img;
  28. $out.="<a href='$link' target='_blank' title=\"".str_replace('"','',$title)."\"><img src='$img'/></a>";
  29. }
  30. return $out;
  31. }
  33. // Example Request with ID and how mant photos required
  34. echo getFlickrFeed("[email protected]",9);


Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.