Facebook graph api photo upload to a fan page album

Posted by kielie on Stack Overflow See other posts from Stack Overflow or by kielie
Published on 2010-06-15T16:21:06Z Indexed on 2010/06/18 3:53 UTC
Read the original article Hit count: 1743

Hi guys, I have gotten the photo upload function to work with this code,

<?php

include_once 'facebook-php-sdk/src/facebook.php';
include_once 'config.php';//this file contains the secret key and app id etc...

$facebook = new Facebook(array(
    'appId'  => FACEBOOK_APP_ID,
    'secret' => FACEBOOK_SECRET_KEY,
    'cookie' => true,
    'domain' => 'your callback url goes here'
));

$session = $facebook->getSession();

if (!$session) {

    $url = $facebook->getLoginUrl(array(
               'canvas' => 1,
               'fbconnect' => 0,
               'req_perms'=>'user_photos,publish_stream,offline_access'//here I am requesting the required permissions, it should work with publish_stream alone, but I added the others just to be safe
           ));

    echo 'You are not logged in, please <a href="' . $facebook->getLoginUrl() . '">Login</a> to access this application';

} else{

    try {

        $uid = $facebook->getUser();
        $me = $facebook->api('/me');
        $token = $session['access_token'];//here I get the token from the $session array
        $album_id = 'the id of the album you wish to upload to eg: 1122';

        //upload your photo
        $file= 'test.jpg';
        $args = array(
        'message' => 'Photo from application',
        );
        $args[basename($file)] = '@' . realpath($file);

        $ch = curl_init();
        $url = 'https://graph.facebook.com/'.$album_id.'/photos?access_token='.$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 id of the photo you just uploaded
        print_r(json_decode($data,true));

    } catch(FacebookApiException $e){
        echo "Error:" . print_r($e, true);
    }
}
?>

I hope this helps, a friend and I smashed our heads against a wall for quite some time to get this working!

Anyways, here is my question, how can I upload a image to a fan page? I am struggling to get this working, when I upload the image all I get is the photo id but no photo in the album.

So basically, when the user clicks the upload button on our application, I need it to upload the image they created to our fan page's album with them tagged on it.

Anyone know how I can accomplish this?

© Stack Overflow or respective owner

Related posts about php

Related posts about facebook