3

Here is the code:

$file= 'bbbb.jpg';
$data = array(
            basename($file) => "@".realpath($file),
            "caption" => "Uploaded using graph api",
            "aid" => '13595',
            "access_token" => $accessToken,
            'method' => 'photos.upload'
);
$sds =$facebook->api($data);

This is the error

Uncaught CurlException: 26: failed creating formpost data

What to do?

Pavel Chuchuva
  • 22,633
  • 10
  • 99
  • 115
Srujana
  • 89
  • 1
  • 2
  • 5
  • This error seems to be related to the permissions on the file you try to upload, are you sure the user that run the PHP script has permissions on the file ? – Serty Oan Jun 03 '10 at 09:34

3 Answers3

12

Here are some various ways to upload photos using the Graph API. The examples assume you've instantiated the $facebook object and have a valid session for the current user.

1 - 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.

$args = array('message' => 'Photo Caption');
$args['image'] = '@' . realpath($FILE_PATH);

$data = $facebook->api('/me/photos', 'post', $args);
print_r($data);

2 - Target Album

This example will upload the photo to a specific album.

$args = array('message' => 'Photo Caption');
$args['image'] = '@' . realpath($FILE_PATH);

$data = $facebook->api('/'. $ALBUM_ID . '/photos', 'post', $args);
print_r($data);

3 - Target Album with Access Token

This example will upload a photo to a specific album which requires an access token.

 $args = array('message' => 'Photo Caption');
$args['image'] = '@' . realpath($FILE_PATH);

$data = $facebook->api('/'. $ALBUM_ID . '/photos?access_token='. $ACCESS_TOKEN, 'post', $args);
print_r($data);
Brody Robertson
  • 8,506
  • 2
  • 47
  • 42
3

your $data array should have "message" instead of "caption", also, remove "aid", "method", and "access_token"
your $data has to have the file data and "message", that is it.

$sds =$facebook->api('/me/13595/photos', 'POST', $data);

where instead of 13595 just use the variable with the album aid

also, if needed, access_token is best appended to api uri like this:

$sds =$facebook->api('/me/13595/photos?access_token='.$access_token, 'POST', $data);

also, if the php sdk doesn't work for you, I have successfully used cURL instead if your php installation supports it. in that case see cURL example at Upload Photo To Album with Facebook's Graph API

Community
  • 1
  • 1
hndcrftd
  • 3,180
  • 1
  • 21
  • 18
2

The latest version of the Facebook PHP SDK wont work with the above examples without the following update to the code.

class Facebook {
...
*Line #539*
protected function makeRequest($url, $params, $ch=null) {
if (!$ch) {
  $ch = curl_init();
}

if( isset($params['doMultiPart']) ) {
    $doMultiPart= true;
    unset($params['doMultiPart']);
} else {
    $doMultiPart= false;
}

$opts = self::$CURL_OPTS;
$opts[CURLOPT_POSTFIELDS] = $doMultiPart ? $params : http_build_query($params, null, '&');
...

Basically the problem is that the PHP SDK uses "curl_setopt_array" which if you pass it a url encoded string as the option value it will pass the data as application/x-www-form-urlencoded when what you really want is multipart/form-data; to do this we simply switch to passing in the array of options if we have a param of doMultiPart in the params array.

This was a quick hack I put together to get something working, probably need to review the code to make sure it doesnt break anything else you are doing. Otherwise enjoy.