Upload Photo To Album with Facebook's Graph API
Solution 1:
Here are some various ways to upload photos using the PHP Facebook Graph API. The examples assume you've instantiated the $facebook object and have a valid session.
1 - Upload to 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.
$facebook->setFileUploadSupport(true);
$args = array('message' => 'Photo Caption');
$args['image'] = '@' . realpath($FILE_PATH);
$data = $facebook->api('/me/photos', 'post', $args);
print_r($data);
2 - Upload to Target Album
This example will upload the photo to a specific album.
$facebook->setFileUploadSupport(true);
$args = array('message' => 'Photo Caption');
$args['image'] = '@' . realpath($FILE_PATH);
$data = $facebook->api('/'. $ALBUM_ID . '/photos', 'post', $args);
print_r($data);
Solution 2:
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
Solution 3:
You have to do a few things to get the graph api to work with php. This code works. Notice the fileUpload => true...
I also was never able to get it to work with javascript so I'm doing ajax to this:
require './facebook.php';
$facebook = new Facebook(array(
'appId' => 'ID',
'secret' => 'SECRET',
'fileUpload' => true,
'cookie' => true // enable optional cookie support
));
$facebook->setFileUploadSupport(true);
# File is relative to the PHP doc
$file = "@".realpath("../../_images/stuff/greatness.jpg");
$args = array(
'message' => 'Photo Caption',
"access_token" => "urtoken",
"image" => $file
);
$data = $facebook->api('/ALBUMID_GOES_HERE/photos', 'post', $args);
if ($data) print_r("success");