[Drupal] How to share a link to user wall with the uploaded photo in Facebook from Drupal sites

| | 2 min read

In this article you can get an idea about how to share a link to user wall with the uploaded photo in Facebook from Drupal sites. To share a link on Facebook, we need to use the Facebook API so we have to download Facebook SDK for PHP. Facebook SDK allows developers to get access to the Facebook API. First, you need to create an app (https://developers.facebook.com/apps), because to reach Facebook we need app id and a secret code.

To share a link with image on Facebook, the image shall be on Facebook (for dynamic image). So first you have to upload a photo or upload a photo in the album. The below code will upload an image on Facebook, but not display in user wall. Because our need is to share a link with an image, So we hide the image from user wall.


  // Include the php sdk file.
  require_once('php-sdk/facebook.php');
  $config = array(
    'appId' => 'YOUR_APP_ID',
    'secret' => 'YOUR_APP_SECRET',
  );
  $message; // Comment to post on your wall.
  $facebook = new Facebook($config);
  // To create an album call this function.
  $album_id = create_facebook_album($facebook);
  // Base url of site.
  $facebook->setFileUploadSupport($base_url);
  // Access Token
  $access_token_ct = $facebook->getAccessToken();
  // Path to the photo on the local filesystem
  $photo = './report/' . $image_name; 
  $photo_obj = $facebook->api('/me/photos', 'POST', array(
    'source' => '@' . $photo,
    'message' => $message,
    'no_story' => TRUE,
    'access_token' => $access_token_ct
  )
);

Now you have to fetch the uploaded image details from Facebook using the below code.


// This will get the image details
$photo_details = $facebook->api('/' . $photo_obj['id']);
$params = array(
  "access_token" => $access_token_ct, 
  "message" => $message,
  "description" => $message,
  "link" => 'http://example.com',
  "picture" => $photo_details['picture'],
  "name" => $page_name, // Name of page
  "caption" => "http://example.com",
);
$facebook_share = $facebook->api('/me/feed', 'POST', $params);

By using the above code you can share a link with image on Facebook. Try this.....