nazly.me

Weblog of Nazly Ahmed

Nazly Ahmed

Nazly Ahmed

Web Developer. PHP Addict. Wordpress Hacker. FOSS Enthusiast. (Micro)Blogger. Photo Hobbyist. Cricket Fanatic. Husband. Dad.

As you would have noticed I have added a section called 'Recent Photos' on the right column of my blog. That shows the recent 5 photos that I have uploaded to my Flickr Photos. To grab my recent photos I'm using phpFlickr, the API kit for Flickr API.

PHPFlickr is so easy to use and has some very basic examples on how to play around with it. PHPFlickr requires PEAR HTTP_Request package. If caching is needed the PEAR DB package is required as well. I'm using caching in my case.

You will need to get an API key from Flickr before using the Flickr API. You can get it from here.

Here goes the code.

Display my recent photos

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
require_once("phpFlickr.php");
#Replace <api_key> with your API Key 
$f = new phpFlickr("<api_key>");
 
#Enable phpFlickr Cache
$f -> enableCache("db", "mysql://user:pass@localhost/db", 1800);
 
#Find my NSID
$nsid = $f->people_findByUsername("nazly");
 
// Get the friendly URL of the my photos
$photos_url = $f->urls_getUserPhotos($nsid);
 
// Get the my recent 5 photos
$photos = $f->people_getPublicPhotos($nsid, NULL, 5);
 
// Display Thumbnail pics
foreach ($photos['photo'] as $photo) {
        echo "<a href=\"".$photos_url.$photo["id"]."\">";
        echo "<img border=\"0\" alt=\"".$photo["title"]."\" ".
        	"src=\"".$f->buildPhotoURL($photo, "Thumbnail")."\" />";
        echo "</a>";
        echo "<br /><br />\n";
}
?>

Display my photos that belongs to specific tag(s)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
require_once("phpFlickr.php");
#Replace <api_key> with your API Key 
$f = new phpFlickr("<api_key>");
 
#Enable phpFlickr Cache
$f -> enableCache("db", "mysql://user:pass@localhost/db", 1800);
 
#Find my NSID
$nsid = $f->people_findByUsername("nazly");
 
// Get the friendly URL of the my photos
$photos_url = $f->urls_getUserPhotos($nsid);
 
// Search for photos
$photos = $f->photos_search(array("tags" => "ubuntu",
					"user_id" => $nsid));
// Display Thumbnail pics
$i=0;
foreach($photos['photo'] as $photo){
	echo "<a href=\"".$photos_url.$photo["id"]."\" target=\"blank\">";
        echo "<img border=\"0\" alt=\"".$photo["title"]."\" ".
        	"src=\"".$f->buildPhotoURL($photo, "Thumbnail")."\" />";
        echo "</a>&nbsp;\n";
        $i++;
        // If it reaches the fifth photo, insert a line break
        if ($i % 5 == 0){
            echo "<br />\n";
        }
}
?>
Posted on 4th August 2006 10:52:21