[Drupal] How to upload a photo to a user's timeline in Facebook from a Drupal website?

By | 21st Mar 2014 | 2 min read

I had a requirement to post an image on a Facebook user's timeline from our Drupal website. In submitting a Drupal form, I had to post an image on Facebook. We implemented this feature using the Facebook SDK for PHP and Drupal. Read on to know how to upload a photo to a user's timeline in Facebook from a Drupal website?

We need to access Facebook api so we have to download Facebook SDK for PHP. Facebook SDk allows developers to access the Facebook api ie to post the image on Facebook. Also create an app on Facebook, once you create an app it will generate a Facebook app id and Facebook secret code. To upload a photo , we have to enable the file upload functionality.


      // Include the facebook sdk for php.
      require_once('php-sdk/facebook.php');
      // Give the app id and secret code that generate while you created a app on facebook.
      $config = array(
        'appId' => 'YOUR_APP_ID',
        'secret' => 'YOUR_APP_SECRET',
        'fileUpload' => true,
      );
      $base_url; // Base url of your server.
      $image_name; // Name of image.
      $facebook = new Facebook($config);
      $facebook->setFileUploadSupport($base_url);
      $photo = './image/' . $image_name; // Path to the photo on the local filesystem
      $ret_obj = $facebook->api('/me/photos', 'POST', array(
          'source' => '@' . $photo,
          'message' => $message,
        )
      );
   

If you implement the above code, you can post images on Facebook from your site.