如何使用Graph API批处理请求上传内联图像? [英] How to Upload Inline-Images using Graph API Batch Request?

查看:125
本文介绍了如何使用Graph API批处理请求上传内联图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Graph API Batch Request上传图片,但无法使用内联图片上传,任何人都可以帮我吗?



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



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



照片批量上传博客帖子代码工作正常,但我想从我的服务器上传图片,而不是从我的电脑,例如:/public_html/image/pic.jpg,我不知道我可以做什么。 / p>

编辑:我正在使用PHP SDK 3.0.1



这是我的代码:

 <?php 
代码已更改,问题已修复,请参阅以下答案
?>

这是从他们的文档:


上传二进制数据



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



以下示例显示如何上传2张照片在一个批次中
调用:




  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'file1=@cat.gif'\
-F'file2=@dog.jpg'\
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:

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

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批处理请求上传内联图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆