将多张照片作为一个批次发布到Facebook页面

时间:2018-05-23 09:42:54

标签: php facebook facebook-graph-api facebook-php-sdk

我需要帮助。我从数据库中通常选择了一组照片。我循环一个数组说$ piclinks然后环顾四周发布图片。但我需要它显示为图像Exactly how i want the pics to appear on facebook page中显示的一个帖子。

任何人都知道我能做什么

 <?php


    define('FACEBOOK_SDK_V4_SRC_DIR', __DIR__.'/src/Facebook/');
require_once(__DIR__.'/src/Facebook/autoload.php');

        $fb = new Facebook\Facebook([
         'app_id' => '2082153322064454',
         'app_secret' => '7355d8111164630537a35b43a1bbd336',
         'default_graph_version' => 'v2.2',
        ]);

        $piclinks = array("http://d24w6bsrhbeh9d.cloudfront.net/photo/agydwb6_460s.jpg", "https://greenparrotnews.com/wp-content/uploads/2017/10/Boss-Mustapha.jpg");
        foreach($piclinks as $selectedlink){

        //Post property to Facebook
            $linkData = [
             'message' => 'Your message here',
             'url' => $selectedlink
            ];
            $pageAccessToken ='EAAdltRkhKkYBAHbLBwoKDZBSat5ulIJZBbMz4gZBQSZCZCY7oH7vRgN16QBuYZBZAjSzZCvt04ypoRaTG4o5jdhXA9bjIV8ZB0ZAJ8lsVNZBxYRInwn7tc8ZBsmdhxKZBnmHCA0n3k3wOOlQRcpDPQyXa1RZBoSb3ZAasDhHHxQfTeRdPObE68OZAAxMoaDcs';


            try {
             $response = $fb->post('/me/photos', $linkData, $pageAccessToken);
            } catch(Facebook\Exceptions\FacebookResponseException $e) {
             echo 'Graph returned an error: '.$e->getMessage();
             exit;
            } catch(Facebook\Exceptions\FacebookSDKException $e) {
             echo 'Facebook SDK returned an error: '.$e->getMessage();
             exit;
            }
            $graphNode = $response->getGraphNode();
            echo 'done';

            }

?>

1 个答案:

答案 0 :(得分:1)

您必须先上传照片,然后才能获得照片ID。之后,您必须使用所有照片ID创建帖子。它可能对你有所帮助。示例代码:

$images = []; // say your all images is in array.
foreach($images as $image){
    try {
        $response = $fb->post("/PAGEID/photos", [
            'source' =>  $fb->fileToUpload($image),
            'published' => 'false'
        ], PAGE_TOKEN);
    } catch(Facebook\Exceptions\FacebookSDKException $e) {
        $errors[]= 'Error: ' . $e->getMessage();
    }

    $graphNode = $response->getGraphNode();
    $post_images[]= $graphNode['id'];
}

foreach ($post_images as $key => $post_image) {
    $attachMedia[$key] = ['media_fbid' => $post_image];
}

try {
    $response = $fb->post(
        "/PAGEID/feed",
        [
            'message' => YOUR_MESSAGE,
            'attached_media' => $attachMedia
        ],
        PAGE_TOKEN

    );
} catch(Facebook\Exceptions\FacebookSDKException $e) {
    $errors[]= 'Error: ' . $e->getMessage();
}

$post = json_decode($response->getBody());