[Drupal] How to upload a video to a fan page of Facebook

| | 1 min read

I had a requirement to post videos on a Facebook fan page from our Drupal website. On submitting a Drupal form, I have to post an video on Facebook. We need to access Facebook api so we have to download Facebook SDK for PHP.

Facebook SDK allows developers to access the Facebook api , i.e to post the video 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 video , 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.
  $module_fb_page_id; // Facebook fan page id.
  $facebook = new Facebook($config);
  $facebook->setFileUploadSupport($base_url);
  $videos = './image/' . $image_name; // Path to the photo on the local filesystem
  $ret_obj_page = $facebook->api('/' . $module_fb_page_id . '/videos', 'POST', array(
        'source' => '@' . $videos,
        'description' => $message,
      )
    );

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