«

»

Feb 10 2008

Learning Flickr APIs: Part II – Uploading Files

After my first article about Flickr APIs, I needed to upload photos to flickr which was a lot harder than I thought it would be. I did some more research on Flickr Libraries for PHP and found one created by Carl of lerdorf.com (Download Flickr library source code). This is a very easy wrapper class that makes the flickr API easy to use.

After you include the flickr wrapper class, you can begin setting up the pages. When setting up your api key, you might want to make the callback_url to be your main page and not what you are posting to. I tried calling everything from one file and posting to that same file, but I ran into the issue with the token. On the initial page, you need to create a token which would give you access to the API and allow for the application to write to flickr. $secrets is just an array with your API key and your API secret.

if(isset($_REQUEST['token'])) {
		$secrets['token'] = $_REQUEST['token'];
		$flickr = new Flickr($secrets);
} else {
		$flickr = new Flickr($secrets);
		if(!isset($_REQUEST['frob'])) {
				$url = $flickr->getAuthUrl('write');
				header("Location: $url");
				exit;
		} else {
				$auth = $flickr->getFrobToken($_REQUEST['frob']);
				$flickr->token = $auth['token'];
		}
}
$check = $flickr->authCheckToken($flickr->token);

Next, you want to setup a form that posts to another page, lets call it “upload.php”. The form should have at least a file input that you can choose your photo. On upload.php, you need to first create a new instance of the Flickr class and then you need to call the token check again to make sure the tokens match. All of the variables are coming from the previous page in which I ask for some information about the photo (title, description, tags). As you can see on line 10, I append a tag of “unapproved” onto every photo that is uploaded by a user so it doesn’t display. Once an administrator approves the photo, I will call a flickr photo method to remove the tag from the photo.

// Upload photo
$perms = array("is_public"=>1);
$fname = $_FILES['photo']['tmp_name'];
$title = $_POST['title'];
$desc  = $_POST['description'];
if($_POST['tags']){
	$tags = $_POST['tags'].", ";
}
$tags .= "unapproved";
echo "uploading...
"; flush(); $photo_id = $flickr->upload($fname,$title,$desc,$tags,$perms,0); if(!$photo_id) { $flickr->showError(); exit; }

Other things you can do with the API is add a photo to sets, give it a note, geotag photos and much more

// Add it to a set named something.
$sets = $flickr->photosetsGetList();
$set_id = false;
if(isset($sets['photosets'])) {
		foreach($sets['photosets'] as $set) {
				if($set['title']==$_POST['which_set']) {
						$set_id = $set['id']; break;
				}
		}
}
if($set_id) {
		$flickr->photosetsAddPhoto($set_id, $photo_id);
} else {
		$set = $flickr->photosetsCreate($_POST['which_set'], $photo_id);
		$set_id = $set['id'];
}

// Add a note
if($_POST['notes']){
	$note_id = $flickr->photosNotesAdd($photo_id,0,0,50,50,$_POST['notes']);
}

I hope this helps, enjoy!

About the author

Timothy Haroutunian

Timothy Haroutunian is a ServiceNow Cloud Implementation Specialist at Acorio. ServiceNow is an IT Management solution that allows for a complete view of your IT and physical environment.

Permanent link to this article: http://www.armenianeagle.com/2008/02/10/learning-flickr-apis-part-ii-uploading-files/

http://pagead2.googlesyndication.com/pagead/show_ads.js

1 comment

  1. Yosip

    Hello Timothy,

    we are developement an aplication with flickr api…we are using your code example and we had some troubles:

    seems like the authentication works and it leads us to the call back url where we fill out the forms and submit the photo. The echo uploading appears in the web page but we can’t see the photo on the flickr!

    We commented the line 10 of the upload.php to remove “unapproved” tag and it doesn’t work.

    the first page is for authenticated flickr:
    http://redimei.com/api_flickr_carl/inicio.php

    and this is for upload photo:
    http://redimei.com/api_flickr_carl/upload.php

    we´d appreciate your help because until now your code was the only one we could get working more or less.
    thank you in advance!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.