19

Now I posting a single photo to wall like this:

$response = $facebook->api("/$group_id/photos", "POST", array(
    'access_token=' => $access_token,
    'message' => 'This is a test message',
    'url' => 'http://d24w6bsrhbeh9d.cloudfront.net/photo/agydwb6_460s.jpg',
   )
);

It works fine, but can I somehow post a multiple photos, something like this:

enter image description here

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Red October
  • 689
  • 2
  • 12
  • 31

5 Answers5

26

You can now publish multiple images in a single post to your feed or page:

For each photo in the story, upload it unpublished using the {user-id}/photos endpoint with the argument published=false.

You'll get an ID for each photo you upload like this:

{
  "id": "10153677042736789"
}

Publish a multi-photo story using the {user-id}/feed endpoint and using the ids returned by uploading a photo

 $response = $facebook->api("/me/feed", 'POST',
  array(
    'access_token=' => $access_token,
    'message' => 'Testing multi-photo post!',
    'attached_media[0]' => '{"media_fbid":"1002088839996"}',
    'attached_media[1]' => '{"media_fbid":"1002088840149"}'
  )
);

Source: Publishing a multi-photo story


EDIT: the example for the proposed solution is now published at this other link.

madbob
  • 456
  • 1
  • 6
  • 13
Kcoder
  • 3,422
  • 4
  • 37
  • 56
  • 3
    As clearly mentioned in the FB docs here[link:https://developers.facebook.com/docs/graph-api/photo-uploads#upload], this DOES NOT work for PAGES. I tried myself and despite images having uploaded successfully, they simply did NOT appear in the post. So, correct your answer please. – Sayed Apr 09 '17 at 21:59
  • This didn't work for me. See me recent question: https://stackoverflow.com/questions/44245608/new-facebook-post-doesnt-show-associated-images – coder.in.me May 30 '17 at 09:16
  • that worked, but if I try to update a existing post it throws **Failed to edit object** – MaBi May 04 '18 at 22:31
  • 2
    This also works for making a multi-image post to a specific PAGE using ver. 3.2. (In the past I had difficulties making multiple-image posts to a page via an old version of the API.) – Tim Dearborn Apr 17 '19 at 20:36
  • This works for me for pages, but I can include only photos. I'd like to also put videos among the media used. – Contestosis Jan 16 '23 at 14:31
9

You can make batch requests as mentioned here: https://stackoverflow.com/a/11025457/1343690

But its simple to loop through your images and publish them directly.

foreach($photos as $photo)
{
       //publish photo
}


Edit:

(regarding grouping of photos on wall)

This grouping is done by facebook automatically if some photos are uploaded into the same album.

Currently you cannot create an album in a group via Graph API - it is not supported (as of now), see this bug.

But you can do this - create an album manually, then get the album_id by-
\GET /{group-id}/albums, then use the the code with album_id instead of group_id-

foreach($photos as $photo){
   $facebook->api("/{album-id}/photos", "POST", array(
      'access_token=' => $access_token,
      'name' => 'This is a test message',
      'url' => $photo
      )
   );
}

I've tested it, see the result-

enter image description here

Community
  • 1
  • 1
Sahil Mittal
  • 20,697
  • 12
  • 65
  • 90
  • yes, but in case of a loop it will be different posts visually. Main idea is to keep 4-5 photos in a single "post" (as on attached screenshot). – Red October Apr 11 '14 at 16:13
  • 1
    lol. You dont worry about that. The screenshot that you've shared is from wall. Facebook group them automatically if posted in a short time span. You'll get this too. – Sahil Mittal Apr 11 '14 at 16:18
  • 2
    hmm... Nope, it doesn't merge two pictures, for example. I've just tried. – Red October Apr 11 '14 at 16:36
  • I'm glad that helped. Can you pls check my sol yo your old problem: http://stackoverflow.com/a/23021358/1343690 - that's the right way to do – Sahil Mittal Apr 11 '14 at 21:05
  • not exactly helped, but I'm very grateful for your efforts :) The fact is, I currently writing a service which should do posts like this, and automatically, of course. So I can't force users to create albums manually :) – Red October Apr 11 '14 at 21:30
  • Hmm, but there is nothing we can do for this. If I get any updates on this bug I'll report to you! :) – Sahil Mittal Apr 11 '14 at 21:31
  • Does this group on the News Feed as well? I can get it to group properly on the user's wall, but not on the news feed. – AppDever May 29 '16 at 20:20
  • I have created an album and uploaded photos to that album. However the album doesn't appear at the group feed. How I can create a new post associated to the album? – JCarlosR Dec 02 '17 at 19:21
6

Actually you can upload a multi story photo(I did it using Graph Api and PHP) but the problem comes if you need scheduled this post.Your post is schedule but also it shows on the page's feed.

P.S. I'm using Graph Api v2.9

PHP Code

$endpoint = "/".$page_id."/photos";

foreach ($multiple_photos as $file_url):
array_push($photos, $fb->request('POST',$endpoint,['url' =>$file_url,'published' => FALSE,]));
endforeach;

$uploaded_photos = $fb->sendBatchRequest($photos,  $page_access_token); 

foreach ($uploaded_photos as $photo):
array_push($data_post['attached_media'], '{"media_fbid":"'.$photo->getDecodedBody()['id'].'"}');
endforeach;

$data_post['message'] = $linkData['caption'];

$data_post['published'] = FALSE;

$data_post['scheduled_publish_time'] = $scheduled_publish_time;

$response = $fb->sendRequest('POST', "/".$page_id."/feed", $data_post, $page_access_token);

$post_id = $cresponse->getGraphNode()['id'];
Angie
  • 255
  • 3
  • 7
0

You will need to upload each photo first with published state to false, and then use the ID's of the unpublished photos to the /me/feed endpoint to schedule the photo. The schedule needs to be within the 24 hours from the time the photos are uploaded as facebook deletes all unpublished photos in 24 hours.

Ref: https://developers.facebook.com/docs/graph-api/photo-uploads/

SudarP
  • 906
  • 10
  • 12
-2

There is no way to publish more than one photo in the same graph API call.

See documentation: https://developers.facebook.com/docs/graph-api/reference/user/photos

I am L
  • 4,288
  • 6
  • 32
  • 49