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:
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:
- The customers will be able to send the required information by filling the form
- The information will be saved in a database including the image
- The first thing will be to generate a user access token
- Generate Long-lived user access token
- Generate Long-lived page access token
- I have decided, instead of pushing the photo directly to Facebook, it'll be first approved by the admin
- 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
Getting Dom Elements by Classname
What Does $$ (Dollar Dollar or Double Dollar) Mean in PHP
Force File Download With PHP Using Header()
Upgrading My Encryption Library from Mcrypt to Openssl
"Invalid Parameter Number: Parameter Was Not Defined" Inserting Data
MySQLi_Real_Escape_String() Expects Exactly 2 Parameters, 1 Given
What's the Difference Between Echo, Print, and Print_R in PHP
Performance of Foreach, Array_Map With Lambda and Array_Map With Static Function
How to Send a Get Request from PHP
How to Create an Array For Json Using PHP
Downloading a Large File Using Curl
Generating Unique Random Numbers Within a Range
Remove All HTML Tags from PHP String