Create photo albums and upload photos using the Facebook Graph API
By David Pratt / Tags: facebook, php / 59 Comments / Published: 11-09-10
UPDATE: Thanks to some of the comments below I have been able to revise the code – thanks Luke & Guilherme
I thought I’d share some recent learning’s with using the Facebook Graph API as there seems to be so few examples out there about how to utilise various aspects of its functionality. I will explain how to create a photo album and then insert a photo into it using PHP.
The code example assumes that you have already generated an authenticated session and have the correct permissions (read_stream, publish_stream, photo_upload, user_photos, user_photo_video_tags). If you haven’t already done so, then there is a good example here about how to do so and you can read more about the Facebook permissions on the Facebook dev site.
The following snippet creates a photo album and then uploads a photo into it:
//At the time of writing it is necessary to enable upload support in the Facebook SDK, you do this with the line:
$facebook->setFileUploadSupport(true);
//Create an album
$album_details = array(
'message'=> 'Album desc',
'name'=> 'Album name'
);
$create_album = $facebook->api('/me/albums', 'post', $album_details);
//Get album ID of the album you've just created
$album_uid = $create_album['id'];
//Upload a photo to album of ID...
$photo_details = array(
'message'=> 'Photo message'
);
$file='app.jpg'; //Example image file
$photo_details['image'] = '@' . realpath($file);
$upload_photo = $facebook->api('/'.$album_uid.'/photos', 'post', $photo_details);
You can add more detail to the created album and photo than I have in the example by using additional parameters within the arguments array.
Subscribe via RSS
Twitter
David Pratt September 26th, 2010 at 2:04 am
Yer that setFileUploadSupport caused me no amount of head scratching!