Follow @endyourif rss Facebook LinkedIn

Facebook, Photos, and You! Facebook, Photos, and You!

Published on May 18, 2009

The Facebook Application API is quite extensive and allows you to get and set many different aspects of a user's Facebook information.

Today, we are going to discuss using the Facebook API to create an album, if it doesn't already exist, and add a photo to that album.

I created a simple class that will help us accomplish things.  It's included in full below, you may need to manipulate a few things to make it work for you.  This class is written as a component for CakePHP.  If you are using CakePHP, you can just drop it in your components folder under the controllers folder.  Without further adieu:

// include the api
include_once(APP . 'vendors/facebook-platform/php/facebook.php');
class FacebookApi extends Object {
    var $_instance;
    var $_apikey;
    var $_secretkey;
    var $_userid;
    var $_albumid;
    function __construct($apikey = null, $secretkey = null) {
        $this->_apikey = $apikey;
  $this->_secretkey = $secretkey;
    function setup() {
  // instantiate it
  $this->_instance = new Facebook($this->_apikey, $this->_secretkey);
  // get the logged in user
  $this->_userid = $this->_instance->require_login();
    function get_album_by_name($name) {
  // get all albums
  $albums = $this->_instance->api_client->photos_getAlbums($this->_userid, NULL);
  // loop through them all and check if it exists
  foreach ($albums as $album) {
      if ($album['name'] == $name)
   return $album['aid'];
  return 0;
    function create_album() {
  $this->_albumid = $this->get_album_by_name('My Custom Album');
  // if the album does not exist, create it
  if ($this->_albumid == 0) {
      // create album
      $results = $this->_instance->api_client->photos_createAlbum('My Custom Album', 'My custom description', '', 'everyone', $this->_userid);
      $this->_albumid = $results['aid'];
    function upload_photo($filename) {
  // step 1: call create album, create album will check if it exists and return us the album id
  // step 2: upload photo with album id
  $results = $this->_instance->api_client->photos_upload($filename, $this->_albumid, '', $this->_userid);
  // step 3: delete the photo after upload
  return $results['link'];

Let's breakdown what's happening above.  The first thing we do is include the Facebook library.  We then create our class "FacebookApi" and create several variables that we will need.

The next thing we do is create a constructor the accepts our Facebook API and Secret keys.  If you are unfamiliar with this, visit the Facebook developer area and review some of their documentation.

Our next function is our setup function.  This function instantiates the Facebook library with our API and Secret key.  We then proceed to call the API function to ensure the user is logged in and retrieve the user id of the logged in user.

The next function retrieves the list of albums for the current user.  We then loop through all of the albums and see if our album already exists.  If it does, it returns the album id; otherwise, it returns 0.

Next up is our function to create an album.  The first thing this function does, is call our get album function.  If the album doesn't exist, we proceed to create the album and store the new album's id in our variable.

The last function, puts all of the pieces together.  It begins by calling our create album function and setting our album id for us.  After it creates or sets our album id, we post the photo to that album.  Finally, we remove the file to clean up after ourselves and we  return the link to the new photo back.

That's the basics of our class.  To use our class, we create a controller and call two functions.  The setup function and the upload function.  Below is a simple example of using it:

function connect($filename = null) {
  $filename = APP . WEBROOT_DIR . '/files/' . $filename . '.png';
  // setup the platform
  App::import('Component', 'FacebookApi');
  $this->FacebookApi = new FacebookApi('my api key', 'my secret key');
  // upload the photo
  $link = $this->FacebookApi->upload_photo($filename);
  // redirect to link


Tags: Facebook | CakePHP Tutorial | Facebook API

My Books
ASP.NET MVC 5 With Bootstrap and Knockout.js
Knockout.js Building Dynamic Client-Side Applications
20 Recipes for Programming MVC 3
20 Recipes for Programming PhoneGap
Rapid Application Development with CakePHP