Using Flickr API Kits

Once you get the hang of the APIs using REST, you’ll likely get tired of using it directly in your programming. The details of authorizing users, uploading photos, and managing a cache of Flickr results (to speed up access) are not things you want to deal with all the time.

API kits in various programming languages have been written to make it more comfortable for you to use the API in your language. These tools often express the Flickr API in terms that are more natural for a given language, by abstracting data, maintaining sessions, and taking care of some of the trickier bits of the API.

You can find a list of API kits for Flickr here:

In this section I’ll describe briefly some options of API kits for PHP. Currently, three Flickr API kits are publicized on the Flickr services page. This section shows how to set them up to do a simple example of a working program for each of the API kits. You then need to figure out which is the best to use for your given situation.


This kit,[105]

pear install -of

Here’s a little code snippet to show you its structure:

  require_once 'Flickr/API.php';
  # create a new api object
  $api =& new Flickr_API(array(
      'api_key'  => API_KEY,
      'api_secret' => API_SECRET

  # call a method

  $response = $api->callMethod('', array(
      'tags' => 'flower',
      'per_page' => '10'

  # check the response

  if ($response){
    # response is an XML_Tree root object
    echo "total number of photos: ", $response->children[0]->attributes["total"];
    # fetch the error
    $code = $api->getErrorCode();
    $message = $api->getErrorMessage();

Why might you want to use PEAR::Flickr_API? It’s a simple wrapper with some defining characteristics:

  • There’s not much of an abstraction of the method calls. You pass in the method name. The advantage is that the API will not be out-­of-date with the addition of new Flickr methods. The disadvantage is that one can imagine abstractions that are more idiomatic PHP.

  • You pass in the API key when creating a new Flickr_API object.

  • The response is an XML_Tree root object.[106]

My conclusion is that it makes sense to use one of the newer, richer PHP API kits: phpFlickr or Phlickr; also, more people are actively working on them.


You can find Dan Coulter’s toolkit at It is written in PHP 4, which is currently an advantage, since PHP 5 is not always readily available. Moreover, there seems to be a continued active community around phpFlickr. To install and test the library, following these steps:

  1. Follow the detailed instructions at Download the latest ZIP file from At the time of writing, the latest is the following:[107]

    or the following:

  2. In theory, PEAR should let me install it, but I was not been able to get it to install phpFlickr.[108]phplib directory and renamed the file to phpFlickr.

  3. Copy and paste the following code as a demonstration of working code:

    $api = new phpFlickr(API_KEY, API_SECRET);
    # Get user's ID
    $username = 'Raymond Yee';
    if (isset($_GET['username']))
      $username = $_GET['username'];
    $user_id = $api->people_findByUsername($username);
    $user_id = $user_id['id'];
    print $user_id;

Let’s see how phpFlickr works:

  • The constructor has three arguments: the mandatory API key and two optional parameters, secret and die_on_error (a Boolean for whether to die on an error condition). Remember that you can use the getErrorCode() and getErrorMsg() functions of $api.[109]

  • This is from the official documentation:

    • Apparently, all of the API methods have been implemented in the phpFlickr class.

    • To call a method, remove the flickr. part of the name, and replace any periods with underscores. You call the functions with parameters in the order listed in the Flickr documentation—with the exception of, for which you pass in an associative array.

    • To enable caching, use the phpFlickr::enableCache() function.

Because the naming convention of phpFlickr, which is closely related to that of the Flickr API, you can translate what you know from working with the API pretty directly into using phpFlickr.


Phlickr requires PHP 5 and is not just a facile wrapper around the Flickr API; it provides new classes that significantly abstract the API. There are significant advantages to this approach; if the abstraction is done well, you should be able to program Flickr in a more convenient and natural method in the context of PHP 5 (for example, you can work with objects and not XML, which you can then turn into objects). The downside is that you might need to juggle between the Flickr API’s way of organizing Flickr functionality and the viewpoint of the Phlickr author. Moreover, if Flickr adds new methods, there is a greater chance of Phlickr breaking as a result—or at least not being able to keep up with such changes.

The home page for the project is as follows:

You can get the latest version of Phlickr from here:

The following code is a simple demonstration of Phlickr in action—it uses the flickr.test.echo method:

  ini_get( 'include_path' ) . PATH_SEPARATOR . "/home/rdhyee/pear/lib/php"

require_once 'Phlickr/Api.php';

#insert your own Flickr API KEY here
define('FLICKR_API_KEY', '[API-KEY]''');
define('FLICKR_API_SECRET', '[SECRET]''');

$api = new Phlickr_Api(FLICKR_API_KEY, FLICKR_API_SECRET);
$response = $api->ExecuteMethod(
  array('message' => 'It worked!'));

print "<hi>{$response->xml->message}</h1>";
  documents the objects of the library. To learn more about Phlickr, buy and read Building Flickr Applications with PHP by Rob Kunkle and Andrew Morton (Apress, 2006). Andrew Morton is the author of Phlickr.


Phlickr must be in a folder called exactly Phlickr for operating systems (such as Linux) whose filenames are case-­sensitive.

[106]—this package has been superseded by XML_Serializer (

[108] pear install -of gets me “Could not extract the package.xml file from /home/rdhyee/pear/temp/download/phpFlickr-2.1.0.tar.gz.”