Michael Minter Programmer & Entrepreneur

PHP Flickr Gallery

In my search for finding a simple solution to any problem, I usually cross many and many blogs and articles, discussing whatever topic. But with so much detail that the solution becomes a problem in itself. So I often times end up resolved to research on my own. In this case, I share the simplest programming method for displaying a Flickr gallery, with PHP.

Assembling Your Variables

$api_key  = 'cr79h3t7g387ghc3fh7338ch87c3ch734';
$user_id  = '33822702%40N08';
$per_page = 10;

$xml      = 'http://api.flickr.com/services/rest/?method=flickr.people.getPublicPhotos&api_key='.$api_key.'&user_id='.$user_id.'&per_page='.$per_page;

$flickr   = simplexml_load_file($xml);
foreach($flickr->photos->photo as $p) {
    echo '<a href="http://www.flickr.com/photos/'.$p['owner'].'/'.$p['id'].'">';
    echo '<img src="http://farm'.$p['farm'].'.static.flickr.com/'.$p['server'].'/'.$p['id'].'_'.$p['secret'].'_s.jpg">';
    echo '</a>';

$api_key is part of an authentication device that allows your server and Flickr to “shake hands” sort of speak. You can get your Flickr API key here.

$user_id is how Flickr identifies you. This value can be found in the URL of your Flickr “photostream”. Here’s mine for reference.

$per_page refers to how many photos will be retrieved from your photostream. Though this limiter is optional, the default count is, “all”. Which means if you have 1,000 plus photos, it could be a very bad thing, for your viewer’s browser load time.

$xml is a link to the webfile that renders a list of your available photos based on previously noted variables. This line can be modified later to include even more search parameters. (see: below)

$flickr uses a PHP function called, simplexml_load_file(), which parses an XML file into an object.

In today’s lesson, $flickr becomes an array and we use foreach() to extract the values we need, to display the gallery.


This method is just what I needed. Something small, easily accessible, and scalable. As you experiment with this script, you’ll be afforded the opportunity to use many different features of the API, which Flickr offers. Click here to view a full list.