7

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?

Odyss3us
  • 6,457
  • 18
  • 74
  • 112

2 Answers2

4

Follow the simple Get login url with

$data['url'] = $this->facebook->getLoginUrl(array('scope'=>'publish_stream,read_stream,user_likes,user_photos,manage_pages'));

this will let you post photo and add album in fanpage. Get access token for

$accounts = $this->facebook->api('/'.$PAGE_ID.'?fields=access_token', 'get', array('access_token' => $access_token));
$page_access_token = $accounts['access_token'];

Create album with other detail

            $album_details = array(
                 'message'=> 'Test album',
               'name'=> $today ,//should be unique each time,
            'access_token'=>$page_access_token
            );
            $album_created = $this->facebook->api('/'.$PAGE_ID.'/albums', 'post', $album_details);

Upload photo to a particualar fanpage album

$photo = $this->facebook->api('/'.$album_id.'/photos', 'POST', array(
                                         'source' => "@"."@".realpath(BASEPATH."../".$photopath),
                                         'message' => 'new photo',
                        'access_token' => $page_access_token,
                        'no_story' => 0
                                         )
                                      );

                //print_r( $album_created['id'] );

And change the path as you wish Happy coding

This code works aweesome in this site....why downvoted.?You can post photos to fanpage if you are owner of that page.I have a working copy of that application.

jack
  • 473
  • 5
  • 21
  • 1
    probably because they don't want all of their users to become owners of the page ;) – dwery Jun 21 '13 at 11:03
  • Hello dear, would you please send me a working copy for create album and upload multiple photos. Need a help. thank you. – Desert_king Jan 25 '16 at 07:01
2

This seems to be a bug with the Graph API. See this http://forum.developers.facebook.com/viewtopic.php?id=59063 (can't post links, yet)

zerkms
  • 249,484
  • 69
  • 436
  • 539
gsharma
  • 178
  • 7
  • ah damn... any other ways to try and link photos to a fan page? Is there a way to tag the photos when they are uploaded? – Odyss3us Jun 18 '10 at 10:37
  • Currently there is no way to authenticate based on pages. But FB is releasing a fix on Tuesday that will fix this. I am hoping that fix will also create permissions to upload photos to pages. http://bugs.developers.facebook.com/show_bug.cgi?id=10005 (See comment #85 and #98 in specific) – gsharma Jun 18 '10 at 22:26
  • They updated code. An album can be created now for the fan page, but guess what - you can't upload an image :) See Comments #121, 122, 128 http://bugs.developers.facebook.com/show_bug.cgi?id=10005 – gsharma Jun 25 '10 at 17:45
  • 2
    Well that's just dandy, they fail once again! I seem to know more about their own API than THEY do, lots of developers out there are struggling to get apps working because of stuff like this, I sincerely hope facebook gets their act together soon, this is truly unacceptable! – Odyss3us Jun 28 '10 at 22:01
  • I just tried this.. seems like there is a issue still..is there a work around? – sesmic Aug 29 '11 at 10:48
  • I got this problem solved..pleasse check the answer.I am downvoting above answer as its false thread. – jack Oct 15 '12 at 06:24