Skip to content
November 1, 2010 / ranacse05

Post on Facebook Fan page as Admin


I was working on a project which needs to post feed/status on facebook fan page . Its easy and there are lots of example on web but the requirement was to post as the page admin not the user . There are lots of users on the facebook developers forum asking for this and in the doc http://developers.facebook.com/docs/reference/rest/stream.publish there is one thing enable_profile_selector but there is an easy way .

First of all you have to install the app to the fan page . So first go to the application profile which will be like http://www.facebook.com/apps/application.php?id=XXXXXXXXXXXXX, xx is the application id and click on the Add to my page , from there choose your page and click Add to page. First part done 🙂

Add app to fan page

Now for coding , you have to save the session key of the admin of that page. Be sure that u have the offline_access,publish_stream and manage_pages permission , manage_pages is a new kind of extended permission we need to use to post on behalf of the page admin .

Then set the session and the user id of the admin

    $facebook = new Facebook(APP_ID,APP_SECRET);
    $facebook->api_client->user = $fb_userid;
    $facebook->api_client->session_key = $session_key;

And then call the stream_publish like this


  $link = 'http://www.mylink.com/';

$attachment = array( 'name' => 'Name of the feed',
        'href' => $link,
        'description' => 'Description of the post'
);

$image_link = 'Image Link';
$attachment['media'] = array(array(
                'type'=>'image',
                'src'=>$image_link,
                'href'=>$link
));
$status = json_encode($attachment);

try {
    $facebook->api_client->stream_publish('',$status,'','',$pageid);
}
catch(Exception $e) {
    print_r($e);
}

Thats it , enjoy 🙂

Advertisements

3 Comments

Leave a Comment
  1. great work…..thumbs up

  2. matthew / Feb 7 2011 2:53 pm

    hi Raquibul, how do i get the session_key for the admin user of the fb fan page? does it ever change? those were the questions i had when reading https://ranacse05.wordpress.com/2010/11/01/post-on-facebook-fan-page-as-admin/ thanks,
    matthew

    • ranacse05 / Feb 14 2011 3:24 pm

      Matthew if u asked the user for offline_access the session key will be constant otherwise it’ll change after few hours.

      And when the admin will install your app u can get the session key using $facebook->session_key .

      Hope this answer your question .

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: