Main | Defining CSS Constants with PHP »

December 13, 2005

Include Recent flickr Photos with PHP

Putting your recent flickr photos on your website or blog is a quick and easy way to share them with the world and keep your site looking fresh. flickr gives you two options to do this.

  1. A small Flash movie
  2. Javascript

Both methods have their advantages. The Flash movie is very pretty. It's animated and sure to attract your readers' eye. (Whether or not that's a good thing is up for debate.) The javascript method is quick and dirty and has the added advantage of allowing you to customize the layout using CSS.

However, both ways have a common downfall - they rely on your readers' browser supporting either the Flash plugin or having javascript turned on. Granted, 95% of today's netizens will have at least one of these enabled (if not both), but they have the added problem of relying on the browser to do all the work. (Also, it's worth noting that the html created by flickr javascript is not standards compliant.)

It's my thinking that the server should do most of the heavy lifting. You shouldn't rely on your visitor's browser to do the rendering for you. It may not seem like much, but every extra bit of processing that you push onto the browser can really slow down an older machine or someone who's viewing your site on a handheld device. You want your site to look good and be responsive. Anything less could drive away repeat visitors.

With that said, there's a third alternative - one that doesn't rely on plugins and requires no client-side (browser) processing. By using PHP and a little elbow grease, we can read through the javascript which flickr provides us, pull out the elements we need, and style them to fit any website.

If you're one of those people looking for a quick solution to include on your blog or website, feel free to skip ahead to the finished product. Otherwise, continue reading to see how everything works.

Getting info from flickr

The first step is finding the web address of the flickr javascript that produces your recent photos. We can get this url from the badge that flickr generates for us. You can make your badge by going here and following the instructions. (You'll need to be signed into flickr. Also, make sure you pick a javascript badge, not a Flash one.)

The html that flickr gives you should look something like this:

If you scroll all the way down through that big mess-o-code, you'll see near the bottom:

This is where the javascript actually includes your pictures. Everything else above that is just layout code. Since we're going to be styling this ourselves, we don't need that part. In fact, all we need is the URL.

If you go to that page, you can see exactly what flickr inserts into your webpage. Click here. (Of course, this link will show you my pictures, so don't be confused when you see random strangers.)

View the source of that page in your browser. You'll see that it's a little bit of random javascript (which we can ignore) and a <div> for each of your pictures. Each <div> has all the information we need.

  • The picture's source.
  • The link to the picture's flickr page.
  • And the picture's title.

All we have to do is extract it, and spit it back out in the format we want.

PHP is your friend

This is where PHP comes in to play. Our script will be broken down into the following parts:

  1. Grab the information from flickr.
  2. Pull out the pieces we need (throw away the rest).
  3. Put it back together for our visitors.

First things first, we can grab flickr's HTML like so:

We store our flickr address into the $url variable and then download the contents of that url into $html using the file_get_contents() function.

Next, we need to pull out only the <divs>, ignoring everything else.

preg_match_all() uses regular expressions to search through our html and extract only the <div>...</div> parts. Regular expressions are a great way to proccess text, but they're too complicated for the scope of this article. To learn more about them, visit http://www.regular-expressions.info

PHP will grab all the <divs> and store them into the array $matches.

Our last step is to loop through the first element in $matches and send its contents to the browser.

Voila! We're practically done.

If we leave everything like it is, the script will work fine. Unforunately, it's not standards compliant because flickr doesn't put a trailing slash "/" on their <img> tag. XHTML requires that all tags be closed. Not to worry, it's an easy fix. We can modify the code above to make the change for us.

str_replace() searches through $div and replaces the incorrect "></a>" piece with "/></a>". (Notice the extra slash that was added.) Now we're finished.

Here's the completed code. Or you can download it here.

Making it better

As it stands, the script works fine. However, for a high traffic site, having to pull data from flickr and process it each time could be a real bottleneck. One solution would be to use disk caching.

The basic logic would be "If the we haven't checked for new photos in over an hour, do so now. Otherwise, just send the visitor what we got last time."

Here's the code. I'll leave explaining it as an exercise for the reader ;-)

howto , php | By tylerhall | 02:05 PM

Trackback Pings

TrackBack URL for this entry:
http://chattablogs.com/mt/mt-tb.cgi/27999

Listed below are links to weblogs that reference Include Recent flickr Photos with PHP:

Comments

what a great tutorial! thanks for sharing... can't wait to see what else you have in store...

Posted by: dwayne at December 14, 2005 10:44 AM

Email "Include Recent flickr Photos with PHP" to a friend!

Email this entry to:


Your email address:


Message (optional):