问题描述
我正在使用Graph API Batch Request上传图片,但无法使用内联图片上传,任何人都可以帮我吗?
批量申请文件:
照片批量上传:
照片批量上传博客帖子代码工作正常,但我想从我的服务器上传图片,而不是从我的电脑,例如:/public_html/image/pic.jpg,我不知道我可以做什么。 / p>
编辑:我正在使用PHP SDK 3.0.1
这是我的代码:
<?php
代码已更改,问题已修复,请参阅以下答案
?>
这是从他们的文档:
curl
-F' access_token = ...'\
-F'batch = [{method:POST,\
relative_url:me / photos,\
body :message =我的猫照片\
attached_files:file1\
},
{method:POST,\
relative_url :我/照片,\
body:message =我的狗照片\
attached_files:file2\
},
]'
-F'[email protected]'\
-F'[email protected]'\
https://graph.facebook.com
编辑2:
我看到的第一个问题是,批次不应该是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)']'
);
尝试{
$ 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 ($信息[0] [ '主体']);
}
if($ info [1] ['code'] =='200'){
$ user_albums = json_decode($ info [1] ['body']);
}
echo< pre> User Profile:\\\
;
print_r($ user_profile);
echo\\\
Albums\\\
;
print_r($ user_albums);
echo< pre>;
}
具体说明如何将$ facebook-> api调用格式化...
编辑:
这是一个批量图片上传工作:
$ files = array(
'/data/Pictures/pic1.jpg',
'/data/Pictures/pic2.jpg',
'/data/Pictures/pic3.jpg'
);
//必须将上传支持设置为true
$ facebook-> setFileUploadSupport(true);
$ batch = array();
$ params = array();
$ count = 1;
foreach($ files as $ file){
$ req = array(
'method'=>'POST',
'relative_url'=>'/ me /照片',
'attached_files'=>'文件'$ count
);
//将此请求添加到批处理中...
$ batch [] = json_encode($ req);
//设置要上传的文件的文件路径
$ params ['file'。$ count] ='@'。真实路径($文件);
$ count ++;
}
$ params ['batch'] ='['。 implode(',',$ batch)。 ];
尝试{
$ upload = $ facebook-> api('/','post',$ params);
} catch(FacebookApiException $ e){
error_log($ e);
$ upload = null;
}
//查看结果...
if(!is_null($ upload)){
echo< pre> 。 print_r($ upload,1)。 <预> 中;
echo< hr />;
}
刚刚测试,它的作用就像一个魅力...
I'm trying to upload images using Graph API Batch Request, but i'm unable to upload using inline image, can anyone help me please?
Batch Request Docs: https://developers.facebook.com/docs/reference/api/batch/
Photo batch uploads: http://developers.facebook.com/blog/post/493/
Photo batch uploads blog post code works fine, but i want to upload images from my server and not from my pc, Ex: /public_html/image/pic.jpg and i'm not sure how i can do it.
EDIT: I'm using PHP SDK 3.0.1
Here's my code:
<?php
CODE WAS CHANGED AND THE PROBLEM IS FIXED ALREADY, SEE THE ANSWER BELOW
?>
This is from their docs:
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 ‘[email protected]’ \
-F '[email protected]' \
https://graph.facebook.com
EDIT 2:
The first issue I see is that the Batch should not be part of the URL, but rather part of the params ...
See the crude batch example below:
$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>";
}
Notice specifically how the $facebook->api call is formatted ...
EDIT:
Here is a working batch picture upload:
$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 />";
}
Just tested and it works like a charm ...
这篇关于如何使用Graph API批处理请求上传内联图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!