Scott Boms

A Quick Intro To phpFlickr

In the last few months I’ve had the opportunity to explore the Flickr public APIs using Dan Coulter’s phpFlickr wrapper classes to handle the API calls and database caching to speed things up.

Although the Flickr APIs are constantly evolving, the phpFlickr classes have pretty much kept up with that evolution and made it very easy to search, view and manage your photos and photosets. As a web developer this is pretty handy because it means there’s another option for creating photo galleries or special applications without having to reinvent the wheel in terms of managing photos or managing multiple photo galleries with different content.

Just about everyone I know who’s seen Flickr thinks it’s great, so why not make the most of what it can do for you?

Getting Started/Installation

The phpFlickr classes are simple to use. Start by downloading the latest version and sign-up for a Flickr Developer API key. You’ll need to the API key to interact with the Flickr APIs and it’s helpful for them to understand how the APIs are being used.

Once you’ve download the phpFlickr package, you’ll need to un-tar the file and upload it to your web server (or drop it into your development environment). That’s it for installation, you’re now ready to start working with the classes.


The example we’ll use to get you started in using the class will be simple: find a set of photos based on a particular tag. Here’s the entirety of the code. I’ll explain it in a moment.

  $f = new phpFlickr("YOUR_FLICKR_API_KEY");
  // Variables to pass to the script
  $tag = "YOUR_TAG";
  $username = "USERS_PHOTOS_TO_SEARCH";
  $i = 0; // Counter variable
  $nsid = $f -> people_findByUsername($username);
  $photos_url = $f -> urls_getUserPhotos($nsid);
  $photos = $f -> photos_search(array("user_id" => $nsid, 
    "tag" => $tag, "per_page" => $num, 
    "sort" => "date-posted-desc"));
  if($photos[total] == 0) {
    echo "<p>Sorry, the requested photo(s) could 
      not be found.</p>";
  } else {
    foreach($photos['photo'] as $photo) {
      echo "<a href='" . 
        $photo['owner'] . "/" . $photo['id'] . 
        "/' title='View larger'>";
      echo "<img src='" . $f-&gt;buildPhotoURL($photo, 
      "Medium") . "' alt='$photo[title]' border='0'></a>";

Save the code into a new file named search.php into the phpFlickr directory on your web server or in your test environment and test accessing the file in a browser. You should have a number of images returned if your query found anything that matched.

The basics of what the code does is simple, so let’s look at it line by line.

  • Line 2: Include the phpFlickr classes. This needs to be called before the page content loads.
  • Line 3: Create a new instance of phpFlickr using your API key.
  • Line 5: Provide a $tag variable to search by. This can be any literal string.
  • Line 6: Provide a $num variable to tell the script how many results to return (up to 500).
  • Line 7: Provide a $username to tell the script whose photos to search. This is the full nickname of the user.
  • Line 8: Setup a counter variable to iterate through the results.
  • Line 9: Create an $nsid variable which will hold the NSID of the Flickr user based on their $username.
  • Line 10: Get the chosen user’s base photo URL.
  • Line 11: Execute the API call based on the above parameters.
  • Line 12: Although commented out, this would display the contents of the data array returned from Flickr.
  • Line 13: Start displaying the results.
  • Line 14: Print an error if no results are returned.
  • Line 17: Loop through the results if multiple photos are returned and print out a link to view the photo on Flickr
  • Line 18: Display the Medium sized photo. Other options are “Square”, “Large” and “Original”.
  • Line 19: Add 1 to the counter variable’s current value.

Hopefully this, along with the other examples available will give you a start at using the Flickr API.