…Details…

Icon

Thoughts that's escape, only details.

PHP & FlickrAPI – Photo wall

I wrote a simple code/class for automatic get photos from flickr photostream (by service API)  to compose randomly a nice miniature photowall (you can see an example on my homepage).

First of all you can need API code (only key, we don’t need secret authentication).

Now the code, my class file was called lib/fget.php in here we define variable, class and function construct:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
class Flickr {
//API Key you can get them on http://www.flickr.com/services/apps/create/apply/
private $apiKey = 'xxxx';
//We define our numeric username http://www.flickr.com/services/api/explore/?method=flickr.people.getInfo
private $NSID = '29479498@N05';
//File per page (max file 200 for free account)
private $ppage = '200';public function __construct() {
}public function retrive() {
//For get data we use REST method and serialize option
$getdata = 'http://flickr.com/services/rest/?method=flickr.photos.search&api_key=' . $this->apiKey . '&user_id=' . $this->NSID . '&per_page=' . $this->ppage . '&format=php_serial';
//Get the data
$result = file_get_contents($getdata);
//De serialize for array use
$result = unserialize($result);
return $result;
}
}
?>

Now include the class into a sample page:

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
//Include the class
require_once('lib/fget.php');
//Recall the class
$Flickr = new Flickr;
//Retrive data array
$data = $Flickr->retrive();
//Randomize data
shuffle($data['photos']['photo']);
//Define 0 to the counter (we need this some line down here)
$count = 0;
?>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<head>
<title>deepreflect.net</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.css" rel="stylesheet" type="text/css">
</head>
<body>
<p>
<a href="home.php">
<?php
 
        foreach($data['photos']['photo'] as $photo) { 
        //URL base format for all photo, if you wanna miniature you can modify .jpg into _s.jpg
        // http://farm{farm-id}.static.flickr.com/{server-id}/{id}_{secret}.jpg
 
        //print photo sequence:
	echo '<img alt="click to Enjoy..." src="http://farm' . $photo["farm"] . '.static.flickr.com/' . $photo["server"] . '/' . $photo["id"] . '_' . $photo["secret"] . '_s.jpg">'; 
	//Start the counter
	$count++;
	//Every 13 create a new line
	if ($count == 13) echo '<br>';
	if ($count == 26) echo '<br>';
	if ($count == 39) echo '<br>';
	if ($count == 52) echo '<br>';
	if ($count == 65) echo '<br>';
	if ($count == 78) echo '<br>';
	//Limit print results to 91 photo
	if ($count == 91) break;
}
?>
</a>
</p>
</body>
</html>

Category: Scripting

Tagged: , , , ,

Leave a Reply

You must be logged in to post a comment.

Archive