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












great work…..thumbs up
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 http://ranacse05.wordpress.com/2010/11/01/post-on-facebook-fan-page-as-admin/ thanks,
matthew
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 .