Upload image to an album if exist else create new album - Facebook
After some hardwork I have some code that uploads an image to Facebook, but now the problem is that it always cre开发者_如何学Pythonates a new album.
I want to modify the code so that it only creates an album if there isn't already one with that name; otherwise, just upload the image to the existing album.My code for uploading images: Upload photos to Facebook Album from an app
The API allows you to upload an image to an existing album for your app. Simply POST to https://graph.facebook.com/USER_ID/photos - The photo will be published to an album created for your app. We automatically create an album for your app if it does not already exist. All photos uploaded this way will then be added to this same album.
If you want to add images to a specific album, you must POST to https://graph.facebook.com/ALBUM_ID/photos - The photo will be published to a specific, existing photo album, represented by the ALBUM_ID.
A detailed How-To Guide is here: https://developers.facebook.com/blog/post/498
First you want to check to see if the album you are creating exists, otherwise Facebook will keep generating the same album with the same name.
$album = $facebook->api($param);
if (!$album) {
$album_details = array(
'message'=> 'Your album description goes here',
'name'=> $albumName
);
$create_album = $facebook->api('/me/albums', 'post', $album_details);
$album_uid = $create_album['id'];
} else {
$album_uid = $album[0]['object_id'];
}
Once you've got the album id, you can upload your photo like so:
$photo_details = array(
'message'=> 'Photo Description Goes Here',
'image'=> '@' . realpath($theUploadFile)
);
$upload_photo = $facebook->api('/'.$album_uid.'/photos', 'post', $photo_details);
I wrote a good tutorial that has this feature in it among many others. My photo upload features is built in with a multi-select and allows you to upload photos and videos at the same time. You can view the entire tutorial and download the package here: http://www.epixseo.com/index.php/facebook-php-3-3-1-and-javascript-sdk-graph-api-tutorial/
UPDATE
The $param variable contains the method, which in this case is a fql query, and the actual fql query... In this instance, an album query could be like so:
$param = array(
'method' => 'fql.query',
'query' => 'SELECT object_id FROM album WHERE owner="'.$user.'" AND name="'.$albumName. '"'
);
精彩评论