我正在尝试使用 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
?>
这是来自他们的文档:
Uploading binary data
Binary data can be specified as part of the multipart/mime portion of the batch API request. The batch Graph API allows uploading multiple binary items as part of a batch call. In order to do this, you need to add all the binary items as multipart/mime attachments to your request, and need each operation to reference its binary items using the "attached_files" property in the operation. The "attached_files" property can take a comma separated list of attachment names in its value.
The following example shows how to upload 2 photos in a single batch call:
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
编辑 2:
最佳答案
我看到的第一个问题是 Batch 不应该是 URL 的一部分,而是参数的一部分......
请参阅下面的原始批处理示例:
$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>";
}
请特别注意 $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 />";
}
刚刚测试过,效果很好......
关于php - 如何使用 Graph API 批量请求上传内嵌图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6712098/