Flickr API
I wanted to add the picturing function to my site to allow better management of pictures, cut down on my own storage and allow easier generation of slideshows. API’s do seem the way to go these days as they allow a lot of functionality to a decidacted application. Since most of my site is written with php I decided to look for a php type api, which I found here many thanks to Dan Coulter.
Basically there are a number of things I needed from the API, an upload function that would return a pic id, an authentication function that would allow me to seamlessly show private pictures from my own account, and a presentation function that would present the pictures in different ways.
Authentication Function
To generate the token, Dan has brilliantly created a token generator
May take a couple of goes to get it right, just try and follow the instructions word for word. Then just add the authentication code to the back end.
PHP Backend
require_once(”phpFlickr/phpFlickr.php”);
$f = new phpFlickr(’password’, ’secret’);
$f->setToken(’token’);
Upload Function
At the moment I’m using the synchronous upload as I still havent mastered the ticket handling with the asynchronous function, at the moment it also appears that I have to upload temporarily to my site and transfer the pictures from there.
Form
<form enctype=”multipart/form-data” action=”<?php echo $editFormAction; ?>” method=”POST”
onSubmit=”MM_validateForm(’textfield’,”,’RisEmail’);return document.MM_returnValue” NAME=”resconf”>
<p> </p>
<table align=”center”>
<tr><td><h3>Picture 1</h3></td><td colspan=”5″><input name=”uploaded1″type=”file” value=” ” /></td></tr>
<tr><td><h3>Picture 2</h3></td><td colspan=”5″><input name=”uploaded2″type=”file” value=” ” /></td></tr>
<tr><td><h3>Picture 3</h3></td><td colspan=”5″><input name=”uploaded3″type=”file” value=” ” /></td></tr>
<tr><td><h3>Picture 4</h3></td><td colspan=”5″><input name=”uploaded4″type=”file” value=” ” /></td></tr>
<tr><td><h3>Picture 5</h3></td><td colspan=”5″><input name=”uploaded5″type=”file” value=” ” /></td></tr>
<tr><td><h3>Name (Required)</h3></td><td colspan=”5″><input type=”text”name=”name” width=”300″></td><td> </td></tr>
<tr><td><h3>Email (Required)</h3></td><td colspan=”5″><input type=”text”name=”pubemail” width=”300″></td><td> </td></tr>
<tr><td></td><td>
<input type=”hidden” name=”insert” value=”hiddenin”><input type=”submit” value=”Upload” /></td></tr>
</form>
PHP Backend
require_once(”phpFlickr/phpFlickr.php”);
$f = new phpFlickr(’password’, ’secret’);
$f->setToken(’token’);$editFormAction = $_SERVER[’PHP_SELF’];
if (isset($_SERVER[’QUERY_STRING’])) {
$editFormAction .= “?” . htmlentities($_SERVER[’QUERY_STRING’]);
}if ((isset($_POST[”insert”])) && ($_POST[”insert”] == “hiddenin”)) {
$name=$_POST[’name’];
$email=$_POST[’pubemail’];$uploaddir = “pic/”;
$target1 = $uploaddir . basename( $_FILES[’uploaded1′][’name’]);
if(move_uploaded_file($_FILES[’uploaded1′][’tmp_name’], $target1)) {$up1=1;}$target2 = $uploaddir . basename( $_FILES[’uploaded2′][’name’]);
if(move_uploaded_file($_FILES[’uploaded2′][’tmp_name’], $target2)) {$up2=1;}$target3 = $uploaddir . basename( $_FILES[’uploaded3′][’name’]);
if(move_uploaded_file($_FILES[’uploaded3′][’tmp_name’], $target3)) {$up3=1;}$target4 = $uploaddir . basename( $_FILES[’uploaded4′][’name’]);
if(move_uploaded_file($_FILES[’uploaded4′][’tmp_name’], $target4)) {$up4=1;}$target5 = $uploaddir . basename( $_FILES[’uploaded5′][’name’]);
if(move_uploaded_file($_FILES[’uploaded5′][’tmp_name’], $target5)) {$up5=1;}$f->auth(”write”);
$info=$name;
$desc=$email;
$tag=$name.” “.$email;if ($up1==1) {
$id1 = $f->sync_upload($target1,$info,$desc,$tag,0,0,0);
}if ($up2==1) {
$id2 = $f->sync_upload($target2,$info,$desc,$tag,0,0,0);
}if ($up3==1) {
$id3 = $f->sync_upload($target3,$info,$desc,$tag,0,0,0);
}if ($up4==1) {
$id4 = $f->sync_upload($target4,$info,$desc,$tag,0,0,0);
}if ($up5==1) {
$id5 = $f->sync_upload($target5,$info,$desc,$tag,0,0,0);
}}
Presentation Function
This part is pretty much straight forward, I’m using one of the two methods of caching the pictures as you can see from below. Basically it speeds up the presentation of the pictures for those impatient webbys! Then getting the picture is pretty simple, getting the picid from my database and converting it to a link to get the picture. The picture size can be changed from “small” to “medium” to regular (just remove the option)
Php Backend
require_once(”phpFlickr/phpFlickr.php”);
// Create new phpFlickr object
$f = new phpFlickr(’apicode’, ‘password’);
$f->setToken(’token’);$f->enableCache(”fs”, “TEMPCACHEFOLDER”);
Frontend
<php
$picno=$row_Recordset3[’id’];
photos_getInfo(”$picno”);
$picloc=$f->buildPhotoURL($result, “Small”);
?><img src=”<?php echo $picloc; ?>”>
So that’s basically it, if you’ve any questions just drop me a mail and don’t forget to have a look at







