Posting as Admin is easy but posting as Page requires more work from your side. To post as Page we need the following permissions:

  • publish_stream
  • manage_pages
 

Using the page object

UPDATE: Facebook has extended their page object and now you can retrieve the page access_token directly from the page. The code below uses the latest methods.
UPDATE #2: the below example was broken because for some reason if the admin didn’t grant the app the manage_pages permission, the /$page_id?fields=access_token call is returning the page id instead of an error. And hence, a permission check was added as a work around and a bug was reported.

We need the manage_pages permission to get the page access token so we can post on its behalf. And this is how we do it:

<?php
// This code is just a snippet of the example.php script 
// from the PHP-SDK <https://github.com/facebook/facebook-php-sdk/blob/master/examples/example.php>
require '../src/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => 'app_id',
  'secret' => 'app_secret',
));

// Get User ID
$user = $facebook->getUser();

if ($user) {
  try {
	$page_id = 'page_id';
	$page_info = $facebook->api("/$page_id?fields=access_token");
	if( !empty($page_info['access_token']) ) {
		$args = array(
			'access_token'  => $page_info['access_token'],
			'message'       => "I'm a Page!"
		);
		$post_id = $facebook->api("/$page_id/feed","post",$args);
	} else {
		$permissions = $facebook->api("/me/permissions");
		if( !array_key_exists('publish_stream', $permissions['data'][0]) || 
			!array_key_exists('manage_pages', $permissions['data'][0])) {
			// We don't have one of the permissions
			// Alert the admin or ask for the permission!
			header( "Location: " . $facebook->getLoginUrl(array("scope" => "publish_stream, manage_pages")) );
		}

	}
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl(array('scope'=>'manage_pages,publish_stream'));
}

// ... rest of your code
?>

And the result:

posting as pagePosting as Page

Using the Javascript-SDK

Achieving the same with only client-side technology:

function postToPage() {
	var page_id = '195234433832970';
	FB.api('/' + page_id, {fields: 'access_token'}, function(resp) {
		if(resp.access_token) {
			FB.api('/' + page_id + '/feed',
				'post',
				{ message: "I'm a Page!", access_token: resp.access_token }
				,function(response) {
					console.log(response);
			});
		}
	});
}
Back to top
 

Using /me/accounts connection

If you are building some sort of a CMS where the admin wants to select one of the pages to update its status, then using the /me/accounts is better you can list the pages and save their access_token in one API call.

$page_id = "XXXXXXXXX";
$page_access_token = "";
$result = $facebook->api("/me/accounts");
foreach($result["data"] as $page) {
    if($page["id"] == $page_id) {
        $page_access_token = $page["access_token"];
        break;
    }
}
if( !empty($page_access_token) ) {
	$args = array(
		'access_token'  => $page_access_token,
		'message'       => "I'm a Page!"
	);
	$post_id = $facebook->api("/$page_id/feed","post",$args);
	// sucess...
} else {
	// couldn't find the page!
}

Always communicate with your page fans as Page not as Admin, fans do know (and trust) your page (product) but not necessary you!

Back to top
  • http://cv.zerkms.com zerkms

    Hi ;-)

    We do not need to get “manage_pages” to post “as Page”. Recall our discussion. That time I posted as page having only “publish_stream” permission.

    • http://cv.zerkms.com zerkms

      You can just pass page_id as `uid` parameter and this will do the trick.

      • http://www.masteringapi.com Ibrahim Faour

        This is only with the OLD REST API. Using the Graph API, you need that permission!

  • PK

    it’s a great sample code! it’s work fine.
    do you know how to post it on background schedule.

    • http://www.masteringapi.com Ibrahim Faour

      Posting on a background-schedule would “most likely” needs an offline_access permission and then storing both the user id and access_token in DB and instead of calling with “me” in your request use the user id.

  • http://www.rncreatives.com Ricky Diancin

    Great post! Can you teach me how can I post a video on a facebook page’s wall?

    • http://www.masteringapi.com Ibrahim Faour

      You are going to upload this video? or is it online somewhere (youtube for e.g.)?

      Anyways, a tutorial about uploading photos and videos is coming soon.

      • http://www.rncreatives.com Ricky Diancin

        no, its just a video on youtube. I am planning to post it to my facebook page wall using a code. Currently I have a working script or program but the problem is I can view the video on the page itself. I need to watch the video on the facebook page and not on youtube.

        • http://www.masteringapi.com Ibrahim Faour

          Have you tried adding the url as source or link fields in the $args array?
          I’m not sure, but I think it should work…read this for more info

          • http://www.rncreatives.com Ricky Diancin

            Yes, I did. But its not the same as manually posting a video link on the page’s wall. Using the codes on top: What you get is only a link to youtube, then once you clicked on it you will then be redirected to youtube’s site.
            Using the manual posting: Users can view the video within the page.
            I want that the users will view the video within my page and not leave the page.

  • Codersam

     hi thank you very muhc for your post but I am facing one problem. I have a website and I want to post to my page wall as a page admin. I have added page admin user(hindigan). Now when I am posting to my fan page wall but it is showing my own facebook name. I want to display my page admin “hindigan”. Can you help me how to do that.

    Thanks in advance
    Sam

  • Thasnias

    an error occured.

    “Fatal error: Uncaught OAuthException: An access token is required to request this resource.
    thrown in… “

  • http://www.facebook.com/raufozgen Rauf Özgen

    i am using this code it was working perfect until 9 hours ago. i am getting this error:

    OAuthException: An unexpected error has occurred. Please retry your request later.

    • http://www.masteringapi.com Ibrahim Faour

      I’ve updated my code to reflect the latest SDK and facebook graph update.

  • Antonio Baptista

    Thanks !!!
    Work Fine…

  • Anonymous

    Super rad thank you!

  • AJ Quick

    Thank you for this post. Finally I was able to post as my page from a PHP scipt! This will help me with a LOT of API integrations.

  • frf

    hm, dont know if its the changes on fb or what, but I cant seem to get to publish posts as page from my app… nor from the php script, it keeps publishing them as user… I even have “Always comment and post on your page as The Page, even when using Facebook as User.” selected…

  • frf

    well, seems publishing to the  /pageID/feed  makes it publish as a user, but to /pageID/links  actually publishes it as page….

    • maxleb

      Work for my with /pageID/links to share a link. Thanks

    • coz

      Genius! Thank you, I’ve been at this to long.

  • Oosswwaalldd

    Actually it is not necessary to include 
    ‘access_token’  => $page_access_token, in the $args to post, you can store the Fan Page IDs and Tokens (MySQL or wherever) and then call 

    $facebook->setAccessToken($token);

    right after creating the $facebook object (SDK Only), and using the Page ID as you did in the example

  • Gruese

    Dude, thanks so much – exactly what I’ve been looking for after tedious hours of reading the facebook developer API documents. Mind you, it still makes sense to read those – understanding the Oauth flow can really help you when you’re experiencing problems. Anyway, thanks again!

  • yyy

    “Always communicate with your page fans as Page not as Admin, fans do know (and trust) your page (product) but not necessary you!” made me laugh, true dat :)

  • Gerwin

    Hi, I seem to be unable to post as a page via this script, as facebook won’t grant me an Access_Token while I run facebook as the page, not the admin. Your script runs fine, except I had to get all the parameters from the app’s page in fb developer.
    Works fine as long as I run fb as the admin, but then all the posts come from the admin, not the page.
    Any hints what I might be doing completely wrong?

  • Mail2jatingarg

    Hi, i am a developer in INDIA… i need some help from ur side.. I created a APP on facebook and i already have all required parameter…….. But i want to autorize this app to user…. How can i do this….?????? i am new in facebook API, Plz help me , if u can….

    Thanka in Advance
    Looking forward for your response.
    Thanks
    jatin garg (INDIA)

  • Apathetic012

    Works very well. Awesome tutorial, this helped me understand how the api() function works.

  • http://www.artwerkstadt.de/ Michael

    Dosn’t work for me 

    but if i change:$page_info = $facebook->api(“/$page_id?fields=access_token”); to$page_info = $facebook->api(“/$page_id/?fields=access_token”); it works fine.May someone out there has the same problem ;)

    • http://profile.yahoo.com/OZLN2MRV5LASX57XVWJ22Z2BMM s

       similar problem for me but $page_info = $facebook->api(“/$page_id/?fields=access_token”); doesnt work either

  • http://twitter.com/IM_adventure Vampire Weekend

    I really need to start learning how to use FB and Twitter API ;)

    http://www.squidoo.com/this-is-bucharest

  • Debashis

    I am also trying to post as a page on my page using the php script but failed to do so. Tried with Michael’s suggestion as well. I am not getting the below array as the value of $page_info and so its not proceeding further.

    Array
    (
    [id] => xxxxxxxxxxxxxxxxxxxxx
    )

    Any suggestion will be appreciated.

    • http://www.masteringapi.com Ibrahim Faour

      I have updated the tutorial.

  • jay123

    hello everyone,

    I want to do wallpost on my facebook page without login into account. Is there any way to do this

  • manoj

    Is there anyways to share a link on page as page by share button,like feed dialogue http://developers.facebook.com/docs/reference/dialogs/feed/ which shares on wall of a user.

  • http://twitter.com/poktipop mcbjam

    It ‘s work fine in javascript. Thanks a lot. See example here : http://www.mcbjam.com

  • Haseeb

    So if i do something like the following, should it work to post replies as the page?

    “/$page_id/$comment_id/comment”, “post”, “Hello this is a reply”

  • Haseeb

    So if i do something like the following, should it work to post replies as the page?

    “/$page_id/$comment_id/comment”, “post”, “Hello this is a reply”

  • Rahul Sharma

    Your Code was not working on internet explorer and chrome

  • Rahul Sharma

    Your first part of code on your page not working properly showing blank page..

  • Ömer Kamçılı

    this work it ->

    require ‘src/facebook.php';

    // Create our Application instance (replace this with your appId and secret).
    $facebook = new Facebook(array(
    ‘appId’ => ‘569758506411269’,
    ‘secret’ => ‘1ff9f45d7bf0413b58045d7547c4b0bd’,
    ));

    // Get User ID

    $user = $facebook->getUser();

    if ($user) {
    try {
    $page_id = ‘534373379944361’;
    $page_info = $facebook->api(“/$page_id?fields=access_token”);
    print_r($page_info);

    if( !empty($page_info[‘access_token’]) ) {

    $params = array(
    // this is the main access token (facebook profile)
    “access_token” => $page_info[‘access_token’],
    “message” => “Here is a blog post about auto posting on Facebook using PHP #php #facebook”,
    “link” => “http://www.pontikis.net/blog/auto_post_on_facebook_with_php”,
    “picture” => “http://i.imgur.com/lHkOsiH.png”,
    “name” => “How to Auto Post on Facebook with PHP”,
    “caption” => “www.pontikis.net”,
    “description” => “Automatically post on Facebook with PHP using Facebook PHP SDK. How to create a Facebook app. Obtain and extend Facebook access tokens. Cron automation.”
    );

    try {
    $ret = $facebook->api(‘/’.$page_id.’/feed’, ‘POST’, $params);
    echo ‘Successfully posted to Facebook Personal Profile';
    } catch(Exception $e) {
    echo $e->getMessage();
    }

    } else {

    $permissions = $facebook->api(“/me/permissions”);
    if( !array_key_exists(‘publish_stream’, $permissions[‘data’][0]) ||
    !array_key_exists(‘manage_pages’, $permissions[‘data’][0])) {
    // We don’t have one of the permissions
    // Alert the admin or ask for the permission!
    header( “Location: ” . $facebook->getLoginUrl(array(“scope” => “publish_stream, manage_pages”)) );
    }

    }
    } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
    }
    }

    // Login or logout url will be needed depending on current user state.
    if ($user) {
    $logoutUrl = $facebook->getLogoutUrl();
    } else {
    $loginUrl = $facebook->getLoginUrl(array(‘scope’=>’manage_pages,publish_stream’));
    }

    • Gnani

      Thanks Yor Code Works…!!!

  • Saswat

    Hi all I want to post a status as a page using facebook api. But it is posting as user. If the post is link then it is doing as a page. But how to achive to post a status as a page admin ? please help me.

  • asd asd

    To the author of this post : YOU ARE MY GOD !
    I finally managed to post to my page, BY THE PAGE (not by my real person facebook account) .. thanks !!!