How to post an uploaded photo to wall?

How can I post an uploaded photo to a user's wall/feed?

My application generates an image with the name of the user and some information. It looks like a certificate. I upload this image to the user's profile, but it doesn't appears on the feed. So the friends don't see that. I tried to post it with -

$post = $facebook->api('/me/feed', 'POST', array(...)); 

But I realized, that I can't post Facebook hosted photos. But there are many applications on Facebook that generate images, uploads them and posts them to a wall. But how?

Upvotes: 0

Views: 1455

Answers (2)

Maerlyn
Maerlyn

Reputation: 34107

You can post facebook-hosted pictures onto the user's wall. You need to do a simple wall post, and include the object_attachment parameter, which contains the picture's id. It is documented here, near the bottom of the table.

You can find more details in this blogpost. It also contains the following example:

<?php
  $app_id = "APP_ID";
  $app_secret = "APP_SECRET";
  $post_login_url = "POST_AUTH_REDIRECT_URL";
  $photo_id = "PHOTO_ID";
  $wall_message = "MESSAGE_FOR_POST";
  $wall_link = "LINK_TO_POST";
  $user_id = "USER_ID";

  $code = $_REQUEST["code"];

  // Obtain the access_token with publish_stream permission 
  if (!$code) {
    $dialog_url= "http://www.facebook.com/dialog/oauth?"
      . "client_id=" .  $app_id
      . "&redirect_uri=" . urlencode( $post_login_url)
      .  "&scope=publish_stream";
    echo("<script>top.location.href='" . $dialog_url
      . "'</script>");
  } else {
    $token_url="https://graph.facebook.com/oauth/access_token?"
      . "client_id=" . $app_id
      . "&redirect_uri=" . urlencode( $post_login_url)
      . "&client_secret=" . $app_secret
      . "&code=" . $code;
    $response = file_get_contents($token_url);
    $params = null;
    parse_str($response, $params);
    $access_token = $params['access_token'];

    // POST to Graph API feed endpoint, which is the user's Wall
    $graph_url= "https://graph.facebook.com/" . $user_id . "/feed?"
      . "message=" . urlencode($wall_message)
      . "&link=" . urlencode($wall_link)
      . "&object_attachment=" . $photo_id
      . "&method=POST"
      . "&access_token=" .$access_token;

    echo '<html><body>';
    echo file_get_contents($graph_url);
    echo '</body></html>';
  }
?>

Upvotes: 1

Lix
Lix

Reputation: 47966

As you said, you can not post a Facebook hosted image back to Facebook. As I see it, you have two options.

  1. Download the image to your server and upload it again to the users feed.

  2. Simply post a link to that photo_object on the users feed. Facebook's crawlers will successfully extract the relevant data from that link and you'll still get a preview of the image in the post.

When you query the Graph API with a photo_id -
example - https://graph.facebook.com/10151812415573306 (a public photo on CocaCola's page), you get some information about that image. The data looks something like this -

    {
      "id": "10151812415573306", 
      "from": {
        "name": "Coca-Cola", 
        "category": "Food/beverages", 
        "id": "40796308305"
      }, 
      "picture": "https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash3/521913_10151812415573306_523748375_s.jpg", 
      "source": "https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash3/521913_10151812415573306_523748375_n.jpg", 
      "height": 332, 
      "width": 500, 
       ...
      "link": "https://www.facebook.com/photo.php?fbid=10151812415573306&set=a.99394368305.88399.40796308305&type=1", 
      "icon": "https://s-static.ak.facebook.com/rsrc.php/v2/yz/r/StEh3RhPvjk.gif", 
      "created_time": "2012-07-09T12:00:41+0000", 
      ...

The parameter you need to post on the users feed is the link parameter -
https://www.facebook.com/photo.php?fbid=10151812415573306&set=a.99394368305.88399.40796308305&type=1

When you post that URL on a users feed/wall - the post will contain a preview of the image and a link to the photo itself (within Facebook).

Upvotes: 0

Related Questions