[Drupal] How to tag a page in user wall of Facebook programatically?

By | 19th Jan 2015 | 1 min read

I had a requirement to tag a friends/fans page on the photo uploaded in user wall on Facebook from a Drupal site. I have created a custom module on that create a form to uplaod the image. On page submit, I post the image on facebook user wall and tag the facebook fan's page with the same. If we need to work with facebook, we need PHP SDK. Facebook SDK allows developers to access the Facebook api.

    require_once('php-sdk/facebook.php');
      $config = array(
        'appId' => 'YOUR_APP_ID',
        'secret' => 'YOUR_APP_SECRET',
        'fileUpload' => true,
      );
      $base_url; // Base url of your server.
      $image_name; // Name of image.
      // This is the code to poast image on user wall.
      $facebook->setFileUploadSupport($base_url);
      $photo = './report/' . $image_name; // Path to the photo on the local filesystem
      $photo_obj = $facebook->api('/me/photos', 'POST', array(
          'source' => '@' . $photo,
          'message' => $message,
        )
      ); 
      // This is the code to tag a photo.
      $photo_id = $photo_obj['id'];  // id of the uploaded photo.
      $fan_page_id; // Fan page id.
      $argstag = array('to' => '$fan_page_id');
      $argstag['x'] = 40;
      $argstag['y'] = 40;
      $argstag['access_token'] = $access_token; // Access Token
      $datatag = $facebook->api('/' . $photo_id . '/tags', 'post', $argstag);

Thus we can tag a fan's page with the photo you have uploaded it on user facebook wall.