11

I'm having a strange problem with the graph API, the below code used to work for me adding a post to a users news feed with a thumbnail of the attached photo (referenced in 'object_attachement' parameter).

However now the post is created as expected however the thumbnail is empty. The photo_id I am using exists in the user's photo collection.

Results of below code now.

    $photo_ID = "3415678920211";//Valid Facebook Photo ID...        
    $facebook = new Facebook($config);

    $attachment =  array(
            'access_token' => $user_token,
            'message' => "Test Message",
            'caption' => "THis is a Caption",
            'name' => "Test Name",
            'description' => "This is a description",
            'link' => 'http://url.com/',
            'object_attachment' => $photo_id,
    );

    $response = $facebook->api("/".$userID."/feed/", 'POST', $attachment);

Am I doing something wrong? I am sure this did use to work and wonder if something changed in the API underneath me.

[Update] I noticed that this seems to happen when I specify both link & object_attachment in the same POST. If I remove the link param from the above then I get a slightly better update however this isn't great as the main reason I want this post to exist is for the addition of the link.

Sahil Mittal
  • 20,697
  • 12
  • 65
  • 90
Shaun Bohannon
  • 491
  • 1
  • 4
  • 11
  • when you define give option link then your feed is counted as "link shared" and facebook display message like "username has shared a link via app_name". – hemc4 Dec 21 '12 at 11:44
  • Hi, my question is how to use facebook stored images in the news feed. Your answer suggests using a remote location to store the image which is not a complete answer. I'd like to leave this open. – Shaun Bohannon Sep 08 '13 at 10:14
  • 1
    It didn't answer the question asked so will not mark as accepted. – Shaun Bohannon Nov 27 '13 at 23:59

4 Answers4

4

I am assuming user gave the permission for user_photos / friends_photos. Since you have the photo_id, you may try this.

$pic = $facebook->api("/PHOTO_ID");
$pic_url = $pic->source;

$attachment =  array(
    'access_token' => USER_ACCESS_TOKEN,
    'message'      => "...",
    'caption'      => "...",
    'name'         => "...",
    'description'  => "...",
    'link'         => URL,
    'picture'      => $pic_url
);

ADDED

You need to store the image some where locally ,in a local server . due to this article

Sahil Mittal
  • 20,697
  • 12
  • 65
  • 90
  • +1 ,this also can be achieved only share the link no need to send picture url as 'link'=>$pic->link – hemc4 Dec 21 '12 at 12:13
  • The picture in question is hosted on Facebook as a photo in an album belonging to the App. If you post a link to a photo hosted in the Facebook CDN then the graph API rejects the call with an error along the lines of 'Cannot post images hosted by Facebook'. – Shaun Bohannon Dec 21 '12 at 17:35
  • Then I guess you are left with one option, download the image in your server and pass the link to that picture in API call. – Sahil Mittal Dec 22 '12 at 09:43
3

I think the 'object_attachment'=>$photo_id won't work any more. You should change it to 'picture'=>$photo_url

$photo_Url = "link to your photo";//Valid Facebook Photo ID...        
$facebook = new Facebook($config);

$attachment =  array(
        'access_token' => $user_token,
        'message' => "Test Message",
        'caption' => "THis is a Caption",
        'name' => "Test Name",
        'description' => "This is a description",
        'link' => 'http://url.com/',
        'picture' => $photo_Url,
);

$response = $facebook->api("/".$userID."/feed/", 'POST', $attachment);

You can find more Here

3

you have to permission pubish_stream on your app and then try this using curl:-

$attachment =  array(
        'access_token' => $access_token,
        'message' => 'i m success to using graph api for post wall',
        'name' => 'Wall Post using graph api',
        'link' => 'www.mysite.com',
        'description' => 'Using the Graph API, any Facebook users Wall feed may be accessed by using this URL:',
        'picture'=>'http://example.com/images/noimage.png'
 );
$url = "https://graph.facebook.com/$facebook_id/feed";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  //to suppress the curl output
$result = curl_exec($ch);
curl_close ($ch);
print_r($result)
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44
  • Actually the question is that the picture that he wants to provide that is hosted on facebook, not from some other external link. – Sahil Mittal Dec 25 '12 at 09:05
  • if picture is profile pic then url would be https://graph.facebook.com/" . $facebook_id . "/picture?type=large if other then tell me what type image you have friends or etc – Rakesh Sharma Dec 25 '12 at 11:10
  • You first check at your end, with any facebook picture. Facebook doesn't allow that to do. That's the issue. – Sahil Mittal Dec 25 '12 at 11:13
  • yaa i tried on my end using this code of my picture https://graph.facebook.com/100001758852143/picture?type=large and feed url like this $url = "https://graph.facebook.com/100001758852143/feed"; – Rakesh Sharma Dec 25 '12 at 12:38
2

You are posting to the feed. You should post to the LINK instead.

Post the link to the picture to the link feed, and you should be fine.

https://developers.facebook.com/docs/reference/api/link/

I will post an example in a tick.

---- Later ----

Hmmmm, well the link option isn't satisfactory. You get a thumbnail of the picture, and you can click it to see the picture in full, but its very ugly. I really don't think that is what you are after.

So I did some more experiments, and kept coming up against that CDN error you mentioned. So I googled it and found the following:

Serving Images in Stream Stories Jun 18, 2010 3:21pm

We no longer allow stream stories to contain images that are hosted on the fbcdn.net domain. The images associated with these URLs aren't always optimized for stream stories and occasionally resulted in errors, leading to a poor user experience. Make sure your stream attachments don't reference images with this domain. You should host the images locally.

So there you have it, according to Facebook, you can no longer do what you want to do.

Relaxing In Cyprus
  • 1,976
  • 19
  • 25