3

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:

Syed I.R.
  • 6,180
  • 2
  • 29
  • 41

3 Answers3

8

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

keithhatfield
  • 3,273
  • 1
  • 17
  • 24
  • @dleiftah Hello sir, i have been trying your solution and i don't seem to get it to work. it is giving me (#324) Requires upload file. – Messy Coder Nov 19 '13 at 14:52
  • I needed to upload to a page's photo album, '{$album_id}/photos' not '/me/photos' - worked for me when I added $params['access_token'] = $page_access_token; Thanks a lot!! – Dante Cullari Sep 13 '14 at 09:30
0
// // File you want to upload/post
$post_data['file1'] = "@D:/home/2.jpg";
$post_data['file2'] = "@D:/home/1.jpg";

// Initialize cURL
$ch = curl_init();
// Set URL on which you want to post the Form and/or data
curl_setopt($ch, CURLOPT_URL, $post_url);
// Data+Files to be posted
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
// Pass TRUE or 1 if you want to wait for and catch the response against the request made
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// For Debug mode; shows up any error encountered during the operation
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// Execute the request
$response = curl_exec($ch);
 // echo curl_errno($ch);
 // echo curl_error($ch);
// Just for debug: to see response
echo $response;

This will work for sure its working for me

Captain JK
  • 160
  • 1
  • 7
0

Well, I'm not too sure and I cannot check at the moment, but

http://au.php.net/manual/en/function.curl-setopt.php

Look it up at CURLOPT_POSTFIELDS, it says:

The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ';type=mimetype'. This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data. As of PHP 5.2.0, files thats passed to this option with the @ prefix must be in array form to work.

Here is another CURL example:

CURL PHP send image

So what you need to do is

$queries = array(
    array("method" => "POST", "relative_url" => "me/photos","body" => "message=cool","attached_files" => 'file1')
);

and

$batch = $facebook->api("/?batch=".json_encode($queries)."&file1=@pic.jpg", 'POST');
Community
  • 1
  • 1
Aston
  • 3,654
  • 1
  • 21
  • 18