1

Upload Photos to Flickr with PHP

http://feedproxy.google.com

I have a bit of an obsession with uploading photos to different services thanks to Instagram. Instagram’s iPhone app allows me to take photos and quickly filter them; once photo tinkering is complete, I can upload the photo to Instagram, Twitter, Facebook, and Flickr. This process made me wonder what it would take to upload photos to Flickr using PHP. This post details how you can authenticate and upload photos to Flickr using PHP with phpFlickr.View DemoStep 1: Flickr Application Key CreationJust as with every other API usage, you need to go to Flickr to sign up for an API key. Put together a good description and title, but one key is that you set the callback/redirect URL to the example page (or at least I did).Step 2: Grab phpFlickrphpFlickr is a suggested PHP library by Flickr. The library is not perfect (there seems to be a few long-standing redirect issues) but it does well as an all-purpose Flickr API interface.Step 3: Configuring and AuthenticationIt’s easiest to understand the process by looking at the code:
// Start the session since phpFlickr uses it but does not start it itself
session_start();

// Require the phpFlickr API
require_once('phpFlickr-3.1/phpFlickr.php');

// Create new phpFlickr object: new phpFlickr('[API Key]','[API Secret]')
$flickr = new phpFlickr('[API KEY]','[API SECRET]', true);

// Authenticate; need the "IF" statement or an infinite redirect will occur
if(empty($_GET['frob'])) {
$flickr->auth('write'); // redirects if none; write access to upload a photo
}
else {
// Get the FROB token, refresh the page; without a refresh, there will be "Invalid FROB" error
$flickr->auth_getToken($_GET['frob']);
header('Location: flickr.php');
exit();
}
After starting the session and requiring the phpFlickr API library, an instance of phpFlickr needs to be created, providing it the API key and API secret. With the instance created, an if statement will be employed to react to a frob parameter from Flickr. If no frob is provided, the auth method should be called, which either confirms authentication or redirects the user to the Flickr site for sign in. If a frob is provided, the authentication token is set and the page needs to be refreshed (I’m not sure why the redirect needs to take place, but it was the only way to ensure authentication worked gracefully).Step 4: Uploading a FileUploading an image to Flickr is actually much easier with phpFlickr than authenticating. Uploading a file is as easy as one function call:
// Send an image sync_upload(photo, title, desc, tags)
// The returned value is an ID which represents the photo
$result = $flickr->sync_upload('logo.png', $_POST['title'], $_POST['description'], 'david walsh, php, mootools, dojo, javascript, css');
The sync_upload method allows many parameters, but the image, title, description, and tags are the most prominent. There is also an async_upload with may also be used.phpFlickr also allows simple read access so that you may create a slideshow of photos, tags, etc., so don’t think that phpFlickr is just for uploading!View DemoMy first attempt to create an independent PHP/Flickr script failed due to the need for OpenAuth. phpFlickr does a great job in managing the entire process, and the documentation is decent for getting started. I ran into a few problems with “too much redirection” (fixed by the “if” statement I added) and an “invalid frob” error (cured by the additional redirect) errors but beside those, phpFlickr is the right choice!Upload Photos to Flickr with PHP is a post from: David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.

Read »
Created by admin user 16 weeks 6 hours ago – Made popular 16 weeks 6 hours ago
Category: Web Development   Tags:

Your Ad Here
Domain Sale! $7.99 .com at GoDaddy

User login

Who's online

There are currently 0 users and 1 guest online.