如何使用Graph API批量请求上传内联图像?

Sye*_*.R. 3 php facebook facebook-graph-api

我正在尝试使用Graph API批量请求上传图片,但我无法使用内嵌图片上传,有人可以帮我吗?

批量申请文档:https://developers.facebook.com/docs/reference/api/batch/

照片批量上传:http://developers.facebook.com/blog/post/493/

照片批量上传博客邮政编码工作正常,但我想从我的服务器而不是从我的电脑上传图像,例如:/public_html/image/pic.jpg,我不知道我怎么做.

编辑:我正在使用PHP SDK 3.0.1

这是我的代码:

<?php
   CODE WAS CHANGED AND THE PROBLEM IS FIXED ALREADY, SEE THE ANSWER BELOW
?>
Run Code Online (Sandbox Code Playgroud)

这来自他们的文档:

上传二进制数据

二进制数据可以指定为批处理API请求的multipart/mime部分.批量图API允许上载多个二进制项作为批量调用的一部分.为此,您需要将所有二进制项作为multipart/mime附件添加到您的请求中,并且需要使用操作中的"attached_files"属性来引用其二进制项."attached_files"属性可以在其值中使用逗号分隔的附件名称列表.

以下示例显示如何在单个批处理调用中上载2张照片:

curl 
     –F  ‘access_token=…’ \
     -F  ‘batch=[{“method”:”POST”, \
                  “relative_url”:”me/photos”, \
                  “body”:”message=My cat photo” \
                  "attached_files":"file1" \
                 },
                 {“method”:”POST”, \
                  “relative_url”:”me/photos”, \
                  “body”:”message=My dog photo” \
                  "attached_files":"file2" \
                 },
                ]’
     -F  ‘file1=@cat.gif’ \
     -F 'file2=@dog.jpg' \
    https://graph.facebook.com
Run Code Online (Sandbox Code Playgroud)

编辑2:

kei*_*eld 8

我看到的第一个问题是Batch不应该是URL的一部分,而是params的一部分......

请参阅下面的原油批次示例:

$batch = array();

$req = array(
    'method'       => 'GET',
    'relative_url' => '/me'
);

$batch[] = json_encode($req);

$req = array(
    'method'       => 'GET',
    'relative_url' => '/me/albums'
);

$batch[] = json_encode($req);

$params = array(
    'batch' => '[' . implode(',',$batch) . ']'
);
try {
    $info = $facebook->api('/','POST',$params);
} catch(FacebookApiException $e) {
    error_log($e);
    $info = null;
}
if(!empty($info)){
    if($info[0]['code'] == '200'){
        $user_profile = json_decode($info[0]['body']);
    }
    if($info[1]['code'] == '200'){
        $user_albums  = json_decode($info[1]['body']);
    }
    echo "<pre>User Profile:\n";
    print_r($user_profile);
    echo "\nAlbums\n";
    print_r($user_albums);
    echo "<pre>";
}
Run Code Online (Sandbox Code Playgroud)

请特别注意$ facebook-> api调用是如何格式化的...

编辑:

这是一个工作批量图片上传:

$files = array(
    '/data/Pictures/pic1.jpg',
    '/data/Pictures/pic2.jpg',
    '/data/Pictures/pic3.jpg'
);

//Must set upload support to true
$facebook->setFileUploadSupport(true);

$batch     = array();
$params    = array();
$count = 1;
foreach($files as $file){
    $req = array(
        'method'         => 'POST',
        'relative_url'   => '/me/photos',
        'attached_files' => 'file' . $count
    );
    //add this request to the batch ...
    $batch[] = json_encode($req);

    //set the filepath for the file to be uploaded
    $params['file'.$count] = '@' . realpath($file);

    $count++;
}
$params['batch'] = '[' . implode(',',$batch) . ']';

try{
    $upload = $facebook->api('/','post',$params);
} catch(FacebookApiException $e) {
    error_log($e);
    $upload = null;
}

//View the results ...
if(!is_null($upload)){
    echo "<pre>" . print_r($upload,1) . "<pre>";
    echo "<hr />";
}
Run Code Online (Sandbox Code Playgroud)

刚刚经过测试,它就像一个魅力......