Photobucket, Photos, and You

P

It seems like photo sharing and social network applications are the in thing right now. Let’s take advantage of this. Today I will provide a simple class that will allow you to post photos to a user’s Photobucket account in minutes.

Step 1: Visit http://photobucket.com/developer/register to register for an API key

Follow the steps to create a new application on Photobucket. When you have finished, Photobucket will send you an email with your API and Secret key. Keep this email for later use.

Step 2: Download the PHP API library

Download and extract the API library to your website directory.

Step 3: Create a photobucket_api.php file and place the following code in it:


<?php
// include the api
include_once('PBAPI-0.2.3/PBAPI.php');
class PhotobucketApi extends Object {
var $_instance;
var $_apikey;
var $_secretkey;
var $_userid;
var $_authToken;
var $_authTokenSecret;
var $_albumid;

function __construct($apikey, $secretkey) {
$this->_apikey = $apikey;
$this->_secretkey = $secretkey;

$this->_instance = new PBAPI($this->_apikey, $this->_secretkey);

// if we don't have session keys for our tokens, get them now
if (empty($_SESSION['pb_authToken'])) {
$params = $this->_instance->login('request')->post()->getResponseString();
$this->_instance->loadTokenFromResponse();
$params = $this->parseResults($params);

$this->_authToken = urlencode($params['oauth_token']);
$this->_authTokenSecret = urlencode($params['oauth_token_secret']);

// store them in session
$_SESSION['pb_authToken'] = $this->_authToken;
$_SESSION['pb_authTokenSecret'] = $this->_authTokenSecret;
} else {
$this->_authToken = $_SESSION['pb_authToken'];
$this->_authTokenSecret = $_SESSION['pb_authTokenSecret'];
unset($_SESSION['pb_authToken']);
unset($_SESSION['pb_authTokenSecret']);
}
}

function setup() {
$this->_instance->setOAuthToken($this->_authToken, $this->_authTokenSecret);
$params = $this->_instance->login('access')->post()->getResponseString();
$this->_instance->loadTokenFromResponse();
$params = $this->parseResults($params);

// update the auth tokens
$this->_authToken = $params['oauth_token'];
$this->_authTokenSecret = $params['oauth_token_secret'];

// set the sub domain
$this->_instance->setSubdomain($params['subdomain']);
// get the username
$this->_userid = $params['username'];
}

function get_album_by_name($name) {
$this->_instance->setOAuthToken($this->_authToken, $this->_authTokenSecret);
$contents = $this->_instance->album($this->_userid)->get()->getResponseString();

$parser = xml_parser_create('');
$albums = array();

xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, trim($contents), $albums);
xml_parser_free($parser);

// loop through them all and check if it exists
foreach ($albums as $album) {
if ($album['tag'] == 'album' && isset($album['attributes'])) {
if ($album['attributes']['name'] == $name)
return $album['attributes']['name'];
}
}

return null;
}

function create_album() {
$this->_albumid = $this->get_album_by_name('My Album');
// if the album does not exist, create it
if (!$this->_albumid) {
// create album
$this->_albumid = 'My Album';
$this->_instance->setOAuthToken($this->_authToken, $this->_authTokenSecret);
$params = $this->_instance->album($this->_userid, array('name' =>

$this->_albumid))->post()->getResponseString();
}
}

function upload_photo($filename) {
// step 1: call create album, create album will check if it exists and return us the album id
$this->create_album();

// step 2: upload photo with album id
$this->_instance->setOAuthToken($this->_authToken, $this->_authTokenSecret);
$params = $this->_instance->album($this->_userid . '/' . $this->_albumid)->upload(array('type' => 'image',

'uploadfile' => "@$filename"))->post()->getResponseString();

$parser = xml_parser_create('');
$results = array();

xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, trim($params), $results);
xml_parser_free($parser);

$link = $results[3]['value'];

// step 3: delete the photo after upload
unlink($filename);

unset($_SESSION['pb_authToken']);
unset($_SESSION['pb_authTokenSecret']);

return $link;
}

function parseResults($params) {
$params = explode("&", $params);
$resp = array();
foreach ($params as $param) {
$temp = explode("=", $param);
$resp[$temp[0]] = $temp[1];
}

return $resp;
}

}
?>

Please note: you may need to update the include_once() path for the API library.

Step 4: Create a new file to instantiate and use our class

Below is an example file that instantiates and uses our API:


// Configure Photobucket component
$this->pb_apikey = "xxxxxxxxxxx";
$this->pb_secretkey = "xxxxxxxxxxxxxxxxxxxxxxxxx";
include_once(photobucket_api.php');
$this->PhotobucketApi = new PhotobucketApi($this->pb_apikey, $this->pb_secretkey);
$this->pb_authToken = $this->PhotobucketApi->_authToken;

// redirect users to login to photobucket
Header('Location: http://photobucket.com/apilogin/login?oauth_token=' . $this->pb_authToken);

You will need to update the API key, Secret key, and potentially the include_once() path.

Step 5: Create another new file to upload our file

When you setup your application you need to specify a callback URL. Enter the path to this new page. Below is example code after the user has logged in to post a photo:


// Configure Photobucket component
$this->pb_apikey = "xxxxxxxxxxx";
$this->pb_secretkey = "xxxxxxxxxxxxxxxxxxxxxxxxx";
include_once(photobucket_api.php');
$this->PhotobucketApi = new PhotobucketApi($this->pb_apikey, $this->pb_secretkey);
$this->pb_authToken = $this->PhotobucketApi->_authToken;

$filename = 'file_to_upload_can_be_already_on_drive_or_from_a_file_upload.png';

// setup the platform
$this->PhotobucketApi->setup();
// upload the photo
$link = $this->PhotobucketApi->upload_photo($filename);

// redirect to link

Header('Location: ' . $link);

That’s it, photos will now be added to an album called “My Album”.

Other useful articles

About the author

By Jamie

My Books