Getting Started With phpFlickr

October 13, 2008

Today we are going to take a look at phpFlickr 2.2.0, a PHP class written by Dan Coulter in PHP4. It acts as a wrapper for Flickr's API and its methods return Flickr's XML response as well structured and simple to read arrays. There is a prerequisite for this article, you will need your own Flickr account and/or your own Flickr API.
 

The Setup

Our first step is to visit phpFlickr's homepage or its SourceForge project homepage and download the latest version; at the time of this article I downloaded release is 2.2.0. Next, create a root folder for this article, i called mine, Getting Started With phpFlickr, and extract the downloaded file into this folder. Finally, open your favorite editor and create two empty php files, account.php and example.php.
The Setup, Getting Started With phpFlickr
 

Your Account

Connecting to your Flickr account requires three separate IDs, your API's Key & Secret IDs and your account's User ID. Once you have applied for your own API, Flickr will show you your Key and Secret ID. Your User ID is simply your Flickr web address, http://www.flickr.com/photos/###; if you are like me and created a permanent link to your photostream without writing down that address down, you can use this tool, idGettr - Find your Flickr ID, to get it back. Once you have all three handy, open account.php, create three variables, $_key, $_secret, & $uid , and fill in your accounts' information; save and close your file.

  1. <?php
  2.  
  3. $_key = ""
  4. $_secret = "";
  5. $_uid = "";
  6.  
  7. ?>

 

Connecting To Flickr

We are now ready to use the phpFlickr class to connect to our account. Open example.php and, using include_once(), we begin by importing our account.php file and the class files, followed by making the initial connection to our account using the variable $_flickr.

  1. <?php
  2.  
  3. include_once("account.php");
  4. include_once("phpFlickr-2.2.0/phpFlickr.php");
  5. include_once("phpFlickr-2.2.0/PEAR/HTTP/Request.php");
  6.  
  7. $_flickr = new phpFlickr($_key, $_secret);
  8.  
  9. ?>

 

Your Photostream

As I mentioned in the beginning, a nice feature of phpFlickr is how its methods return Flickr's XML response as nice and simply arrays. For example, lets use the phpFlickr method photosets_getList($user_id = NULL) to connect to Flickr's API method, flickr.photosets.getList, who returns a list of each set in your photostream with its ID, description, number of photos and more. We will use the variable $_photosets to hold our array of information and print_r() to display the results.

  1. <?php
  2.  
  3. include_once("account.php");
  4. include_once("phpFlickr-2.2.0/phpFlickr.php");
  5. include_once("phpFlickr-2.2.0/PEAR/HTTP/Request.php");
  6.  
  7. $_flickr = new phpFlickr($_key, $_secret);
  8. $_photosets = $_flickr->photosets_getList($_uid);
  9. print_r($_photosets);
  10.  
  11. ?>

 
Once you are ready, save your file, upload and/or open example.php on your live or local server. If you have entered the correct information inside account.php, you will see something like mine when choosing View Source on your browser.

  1. Array
  2. (
  3.     [photoset] => Array
  4.         (
  5.             [0] => Array
  6.                 (
  7.                     [id] => 72157607910181389
  8.                     [primary] => 2928529397
  9.                     [secret] => 60a692493f
  10.                     [server] => 3070
  11.                     [farm] => 4
  12.                     [photos] => 2
  13.                     [videos] => 0
  14.                     [title] => Baby Myles Alexander
  15.                     [description] => Pictures of Myles from birth to 12 months
  16.                 )
  17.  
  18.         )
  19.  
  20. )

 

The Outro

This class does require some knowledge of how Flickr's API methods work, what they return and what they require. Thankfully, you can open phpFlickr.php inside our phpFlickr-2.2.0 folder to look at all the methods available in this class. Dan Coulter has commented the associated Flickr API address for you to reference and read more about what that method.

  1. function photosets_getList($user_id = NULL)
  2. {
  3.     /* http://www.flickr.com/services/api/flickr.photosets.getList.html */
  4.     $this->request("flickr.photosets.getList", array("user_id" => $user_id));
  5.     return $this->parsed_response ? $this->parsed_response['photosets'] : false;
  6. }

Leave a Reply

You must be logged in to post a comment.