Photoalbum with Flickr – part 1 testing the API

29 07 2008

The aim

I wanted to have a photoalbum for the buildings and the people who lived in a street. However, I wanted the photoalbum to be as shareable and robust as possible. Thus, instead of building my own, I opted for using Flickr.

Apart from the fact that Flickr is possibly the most popular photosharing network, it has also the great advantage of offering an API which allows seamless integration with your applications. Furthermore, lots of people are contributing to create wrappers on different languages which simplify the API calls.

On this regard, since the project is written in PHP, I decided to adopt phpFlickr for my purposes. At the time of this post, the version is 2.2.0.

Run a preliminary test

The first step was to get the API key . This is free and you can obtain it by logging into your Flickr’s account and clicking on: http://www.flickr.com/services/api/keys/

Once you have completed the registration process, you will be given two codes, which you will need later to access Flickr, an api key and a secret.

I always do my experiments in a local website, which acts like a sandbox (”tests”) for my experiments so that I do damage my actual applications. Thus to test Flickr API, I have created a subfolder “flickrapi” in my tests folder. I have also created an empty file index.php and checked the server sees the new folder:

localhost:8020/flickapi (where localhost:8020/ is my virtual host for the tests folder in Apache)

Next, I have downloaded phpFlickr in the “flickrapi” folder.

To check that the package is seen by php, I have modified flickrapi/index.php with:

<?
include "phpFlickr/phpFlickr.php";
?>

If no errors are displayed, the package has successfully accessed.

The second step was to start using the API itself. phpFlickr implements all the API methods as methods of a Flickr class. So you need to instantiate a Flickr object first, by doing:

$f = new phpFlickr($api_key,$secret,$die_on_error);

where:

$api_key and $secret – are the API key and secret code obtained earlier.

$die_on_error – is a boolean value and determines whether operations should cease if the API returns an error (default is false).

If no error occurred, you can start quering Flickr using the specified methods on $f ( these are defined in phpFilckr.php).

Since the test was successful, I moved on to play a bit with Flickr’s calls. In particular I wanted to:

1. access my photos

2. retrieve pictures according to their tags and machine tags

access to my photos

First, I searched among the people’s database on Flickr, my own account. Then, from my account’s details, I retrieved my user id and using this I got access to my photos.

$username="myusername_example";
  // Find the NSID of the username inputted via the form
    $person = $f->people_findByUsername($username);

    // Get the friendly URL of the user's photos
    $photos_url = $f->urls_getUserPhotos($person['id']);

get photos with specific tags and machine tags

To run a search, I used photos_search method and as the $tags variable I specified the tags/machine tags I wanted to filter my photos by.

//Get photos that have tag "flat"
$photos =$f->photos_search(array("user_id"=>$person['id'],
"tags"=>"flat",
"per_page"=>6));
//Get photos with machine tag building num 2, which represents a building number.
      $photos =$f->photos_search(array("user_id"=>$person['id'], "machine_tags"=>"myhousemystreet:buildingnum=\"2\"", "per_page"=>6));

The last (optional) step for me was to move the phpFlickr package from my test folder “flickrapi” to the folder which contains all my php packages, in my PHP5 installation. Then, I had to modify my code accordingly to check it still worked. This final test allowed me to see that I can now use phpFlickr in any site I want from now on, by just inserting:

require_once incpath("/phpFlickr/phpFlickr.php");

At the top of my php files. This is an optional step, but if I didn’t do that, every time I needed to use Flickr API, I would have had to copy phpFlickr as a folder within my new website.

So here’s my final index.php:

<?
/**
 * Testing connection to Flickr, using API.
 * inspired by http://phpflickr.com/
*/
require_once incpath("/phpFlickr/phpFlickr.php");

$api_key ="c0fde778bd98ac8701f2d4e8ef0be50c";
$secret ="dcf4ae887a9b1a06";
$die_on_error =true;// cease operation) if the API returns an error statement
$f = new phpFlickr($api_key,$secret,$die_on_error);
//SEARCH FOR PHOTOS
$username="val.cartei";
  // Find the NSID of the username inputted via the form
    $person = $f->people_findByUsername($username);

    // Get the friendly URL of the user's photos
    $photos_url = $f->urls_getUserPhotos($person['id']);

	//Get first 10 photos with specified tag in $extras
	  $photos =$f->photos_search(array("user_id"=>$person['id'], "machine_tags"=>"myhousemystreet:bnum=\"2\"", "per_page"=>6));
/*
    // Get the user's first 10 public photos
    $photos = $f->people_getPublicPhotos($person['id'], NULL, 10);
*/
	$i=0;
	  // Loop through the photos and output the html
    foreach ((array)$photos['photo'] as $photo) {
        echo "<a href=$photos_url$photo[id]>";
        echo "<img border='0' alt='$photo[title]' ".
            "src=" . $f->buildPhotoURL($photo, "Square") . ">";
        echo "</a>";
        $i++;
        // If it reaches the sixth photo, insert a line break
        if ($i % 6 == 0) {
            echo "<br>\n";
        }
	}

?>

Resources

http://www.flickr.com/services/api/ – Flickr’s API Official Documentation

http://phpflickr.com/ – the official website for the phpFlickr package.


Actions

Information

Leave a comment