tags:

views:

4259

answers:

2

Hello All,

I'm trying to familiarize myself with Facebook's new Graph API and so far I can fetch and write some data pretty easily.

Something I'm struggling to find decent documentation on is uploading images to an album.

According to http://developers.facebook.com/docs/api#publishing you need to supply the message argument. But I'm not quite sure how to construct it.

Older resources I've read are:

If someone has more information or could help me tackle uploading photos to an album using Facebook Graph API please reply!

+11  A: 

Here is the code that worked for me:

//upload photo
$file= '/path/filename.jpg';
$args = array(
   'message' => 'Photo from application',
);
$args[basename($file)] = '@' . realpath($file);
$ch = curl_init();
$url = 'http://graph.facebook.com/'.$album_id.'/photos?access_token='.$access_token;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);
//returns the photo id
print_r(json_decode($data,true));

Link to documentation: http://developers.facebook.com/docs/reference/api/photo

AhDang
Wow, you're the man AhDang!
st4ck0v3rfl0w
How do you know this? Any documentation from Facebook.com?
Billy
I'd also like to know where you found this! Unless you rolled your own?
Pier-Luc Gendreau
Is this still working for you? There seem to be a problem on the FB platform lately with photo uploads
marcgg
+2  A: 

Here are some various ways to upload photos using the Graph API. The examples assume you've instantiated the $facebook object and have a valid session.

1 - Default Application Album of Current User

This example will upload the photo to your default application album of the current user. If the album does not yet exist it will be created.

$args = array('message' => 'Photo Caption');
$args['image'] = '@' . realpath($FILE_PATH);

$data = $facebook->api('/me/photos', 'post', $args);
print_r($data);

2 - Target Album

This example will upload the photo to a specific album.

$args = array('message' => 'Photo Caption');
$args['image'] = '@' . realpath($FILE_PATH);

$data = $facebook->api('/'. $ALBUM_ID . '/photos', 'post', $args);
print_r($data);