Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Upload Inline-Images using Graph API Batch Request?

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  ‘[email protected]’ \
     -F '[email protected]' \
    https://graph.facebook.com

EDIT 2:

like image 461
Syed I.R. Avatar asked Dec 20 '25 06:12

Syed I.R.


1 Answers

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 ...

like image 76
keithhatfield Avatar answered Dec 22 '25 19:12

keithhatfield



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!