How do I can post a multiple photos via Facebook API

19,325

Solution 1

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

Solution 2

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

Solution 3

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'];
Share:
19,325

Related videos on Youtube

Red October
Author by

Red October

Updated on September 16, 2022

Comments

  • Red October
    Red October over 1 year

    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

  • Red October
    Red October about 10 years
    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).
  • Sahil Mittal
    Sahil Mittal about 10 years
    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.
  • Red October
    Red October about 10 years
    hmm... Nope, it doesn't merge two pictures, for example. I've just tried.
  • Sahil Mittal
    Sahil Mittal about 10 years
    I'm glad that helped. Can you pls check my sol yo your old problem: stackoverflow.com/a/23021358/1343690 - that's the right way to do
  • Red October
    Red October about 10 years
    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 :)
  • Sahil Mittal
    Sahil Mittal about 10 years
    Hmm, but there is nothing we can do for this. If I get any updates on this bug I'll report to you! :)
  • AppDever
    AppDever almost 8 years
    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.
  • Sayed
    Sayed about 7 years
    As clearly mentioned in the FB docs here[link:developers.facebook.com/docs/graph-api/photo-uploa‌​ds#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.
  • coder.in.me
    coder.in.me almost 7 years
    This didn't work for me. See me recent question: stackoverflow.com/questions/44245608/…
  • JCarlosR
    JCarlosR over 6 years
    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?
  • MaBi
    MaBi almost 6 years
    that worked, but if I try to update a existing post it throws Failed to edit object
  • Tim Dearborn
    Tim Dearborn about 5 years
    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.)