Upload Photo to Album With Facebook'S Graph API

Upload Photo To Album with Facebook's Graph API

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);

Facebook Graph API: Uploading Photo to specific album

The aid is not the ID you should be using when you do the upload. This is not obvious from the Facebook docs - but you should use the object_id column as this represents an object on the Graph API.

So, change your FQL query to:

SELECT object_id,can_upload,created,name,description FROM album WHERE owner={0}

The object_id is just a number - not two numbers separated with an underscore. So make your post to:

https://graph.facebook.com/{0}/photos?access_token={1}
  • {0} is the 9999999 object ID
  • {1} is the access token from the login

Note that you only need user_photos permission to upload photos. But without publish_stream permission they do not appear in the user's album straight away - instead, when the user browses to the album they will see this:

Sample Image

Once they click approve they will appear in the album. If your application has publish_stream then they will go straight into the album and the user will not need to approve them.

The rest of your code should be correct.

Facebook graph API: Upload photo to page album

/page-id/albums?name=albumname is for creating a new album with that name.

To publish a photo to an existing album, you have to post to /{album-id}/photos.

See https://developers.facebook.com/docs/graph-api/reference/v2.1/album/photos#publish for more details.

Facebook graph API upload photos to albums

You need to set the upload support to true. The following code works for me:

$facebook->setFileUploadSupport(true);
$facebook->api('/'.$aid.'/photos', 'POST', array('image'=> '@' . realpath($FILE_PATH), 'message'=> 'Photo Caption'));

and furthermore, you will probably need the user_photos, photo_upload, and the publish_stream permissions.

Facebook Graph API - Post a photo in an album with page admin access token

I have reached the conclusion after a few research and suggestions provided by @CBroe that designing the app like the way I initially imagined is not very practical and here is what I have decided to do:

  1. The customers will be able to send the required information by filling the form
  2. The information will be saved in a database including the image
  3. The first thing will be to generate a user access token
  4. Generate Long-lived user access token
  5. Generate Long-lived page access token
  6. I have decided, instead of pushing the photo directly to Facebook, it'll be first approved by the admin
  7. If everything alright the admin will post it to the Facebook album with a push of a button

So, my solution is to automate posting to the Facebook where the admin has to push a approve button instead of the customer being able to directly post the photo on Facebook. I will try to post a link to git after it's done.



Related Topics



Leave a reply



Submit