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:
http://www.flickr.com/services/api/
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 http://code.iamcal.com/php/flickr/Flickr_API-Latest.tgz
Here’s a little code snippet to show you its structure:
<?php include("flickr_key.php"); 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('flickr.photos.search', 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"]; }else{ # 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 http://phpflickr.com/
. 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:
Follow the detailed instructions at http://phpflickr.com/docs/?page=install
. Download the
latest ZIP file from http://sourceforge.net/projects/phpflickr
. At the
time of writing, the latest is the following:[107]
http://downloads.sourceforge.net/phpflickr/phpFlickr-2.1.0.tar.gz
or the following:
http://downloads.sourceforge.net/phpflickr/phpFlickr-2.1.0.zip
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
.
Copy and paste the following code as a demonstration of working code:
<?php include("flickr_key.php"); require_once("phpFlickr/phpFlickr.php"); $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
flickr.photos.search
, 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:
http://drewish.com/projects/phlickr/
You can get the latest version of Phlickr
from here:
http://sourceforge.net/project/showfiles.php?group_id=129880
The following code is a simple demonstration of Phlickr
in
action—it uses the flickr.test.echo
method:
<?php ini_set( 'include_path', 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( 'flickr.test.echo', array('message' => 'It worked!')); print "<hi>{$response->xml->message}</h1>"; ?>
http://drewish.com/projects/phlickr/docs/
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
.
![]() | Note |
---|---|
|
[106] http://pear.php.net/package/XML_Tree—this package has been superseded by XML_Serializer (http://pear.php.net/package/XML_Serializer)
[107] http://sourceforge.net/project/showfiles.php?group_id=139987&package_id=153541&release_id=488387
[108] pear install -of http://downloads.sourceforge.net/phpflickr/phpFlickr-2.1.0.tar.gz gets me “Could not extract the package.xml file from /home/rdhyee/pear/temp/download/phpFlickr-2.1.0.tar.gz.”