How do I can post a multiple photos via Facebook API
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-
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'];
Related videos on Youtube
Red October
Updated on September 16, 2022Comments
-
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:
-
Red October about 10 yearsyes, 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 about 10 yearslol. 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 about 10 yearshmm... Nope, it doesn't merge two pictures, for example. I've just tried.
-
Sahil Mittal about 10 yearsI'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 about 10 yearsnot 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 about 10 yearsHmm, but there is nothing we can do for this. If I get any updates on this bug I'll report to you! :)
-
AppDever almost 8 yearsDoes 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 about 7 yearsAs clearly mentioned in the FB docs here[link: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.
-
coder.in.me almost 7 yearsThis didn't work for me. See me recent question: stackoverflow.com/questions/44245608/…
-
JCarlosR over 6 yearsI 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 almost 6 yearsthat worked, but if I try to update a existing post it throws Failed to edit object
-
Tim Dearborn about 5 yearsThis 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.)