简体   繁体   English

如何通过 Facebook API 发布多张照片

[英]How do I can post a multiple photos via Facebook API

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:它工作正常,但我可以以某种方式发布多张照片,如下所示:

在此处输入图片说明

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 .对于故事中的每张照片,使用{user-id}/photos端点和参数published=false上传未发布的{user-id}/photos

You'll get an ID for each photo you upload like this:您上传的每张照片都会获得一个 ID,如下所示:

{
  "id": "10153677042736789"
}

Publish a multi-photo story using the {user-id}/feed endpoint and using the ids returned by uploading a photo使用{user-id}/feed端点并使用上传照片返回的 id 发布多照片故事

 $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来源: 发布多照片故事

You can make batch requests as mentioned here: https://stackoverflow.com/a/11025457/1343690您可以按照此处所述进行批量请求: 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 .如果将某些照片上传到同一个相册中,则此分组由 facebook 自动完成。

Currently you cannot create an album in a group via Graph API - it is not supported (as of now), see this bug .目前您无法通过 Graph API在群组中创建相册 - 不支持(截至目前),请参阅此错误

But you can do this - create an album manually, then get the album_id by-但是您可以这样做 - 手动创建专辑,然后通过以下album_id获取专辑album_id -
\\GET /{group-id}/albums , then use the the code with album_id instead of group_id - \\GET /{group-id}/albums ,然后使用带有album_id而不是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-我已经测试过了,看看结果——

在此处输入图片说明

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.实际上,您可以上传多故事照片(我使用 Graph Api 和 PHP 完成),但是如果您需要安排此帖子,问题就会出现。您的帖子已安排好,但也会显示在页面的供稿中。

PS I'm using Graph Api v2.9 PS我使用的是Graph Api v2.9

PHP Code PHP代码

$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'];

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.您需要先上传每张照片,并将发布状态设为 false,然后使用未发布照片的 ID 到 /me/feed 端点来安排照片。 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.时间表需要在照片上传后的 24 小时内,因为 facebook 会在 24 小时内删除所有未发布的照片​​。

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

There is no way to publish more than one photo in the same graph API call.无法在同一个图形 API 调用中发布多于一张照片。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM