Upload a photo to an album using the Facebook API

I am trying to familiarize myself with the new Facebook API, and so far I can easily and easily retrieve and record some data.

Something I'm trying to find decent documentation with is uploading images to an album.

According to http://developers.facebook.com/docs/api#publishing you need to provide a message argument. But I'm not quite sure how to build it.

Previous resources that I read:

If anyone has more information or you can help me upload photos to an album using the Facebook API, please reply!

+43
php facebook
Apr 27 '10 at 3:55
source share
3 answers

Here are some ways to upload photos using the PHP Facebook Graph API. The examples assume that you created an instance of the $ facebook object and have a live session.

1 - Download the default application album of the current user

In this example, you upload the photo to your default application album of the current user. If the album does not exist yet, 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

In this example, 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); 
+48
Jun 09 2018-10-06
source share

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

Documentation link: http://developers.facebook.com/docs/reference/api/photo

+22
Apr 28 '10 at 9:39 on
source share

You have to do a few things to get an api chart for working with php. This code works. Pay attention to fileUpload => true ...

I also could never get it to work with javascript, so I am doing ajax for 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"); 

code>

+7
Jan 29 '11 at 4:50
source share



All Articles