Posting to FB pages from web application

This is the code:

$appId = 'YOUR APP ID';
$secret = 'YOUR SECRET';
$returnurl = '';
$permissions = 'manage_pages, publish_stream, offline_access';

$fb = new Facebook(array('appId'=>$appId, 'secret'=>$secret));
$fbuser = $fb->getUser();


$page_id = "YOUR PAGE ID";
$page_access_token = "GET PAGE TOKEN ID";
$result = $fb->api("/me/accounts");

// loop trough all your pages and find the right one
if( !empty($result['data']) )
foreach($result["data"] as $page)
if($page["id"] == $page_id)
$page_access_token = $page["access_token"];
echo "AN ERROR OCCURED: could not get the access_token. Please verify the page ID ".$page_id." exists.";

// set the facebook active facebook access token as the one we just fetch

// Now try to post on page's wall
$message = array(
'message' => "YOUR MESSAGE",
'name' => "Website Title",
'picture' => "YOUR PICTURE",
'description' => "YOUR DESCRIPTION",
'link' => "YOUR LINK"
$result = $fb->api('/'.$page_id.'/feed','POST',$message);
echo 'Successfully posted to Facebook Wall...';
}catch(FacebookApiException $e){
echo $e->getMessage();


$fbloginurl = $fb->getLoginUrl(array('redirect-uri'=>$returnurl, 'scope'=>$permissions));
echo 'Login with Facebook';


of course you will need the SDK from here:

and add this line, such as:
require ‘facebook-php-sdk/src/facebook.php’;

but, before that, you will need to create FB app and get the app ID and app secret.

You will also need the access token id. How to get it? Follow this steps:

1. Go to to create a new facebook application. Omit the hosting offer, you create just an envelop to get your own App ID and App Secret (=password).

2. In Settings, go to Basic settings and select Website with Facebook Login option. The Site URL must link to the website that does the authorization. Disable the Sandbox Mode.

3. Still in settings, go to Permissions. Set the permissions, I need read_stream, manage_pages.

4. Using an URI like this,{your app’s id}&redirect_uri={your auth script}, ask the user for permissions. After he agrees, it will redirect his browser to redirect_uri, giving you his authorization code via HTTP get params. The code is single-use, don’t store it, just use it in the next step.

5. Ask for access_token using the URI below. It contains App Secret, do it server-side. Beware of redirect_uri. It is as bothersome as it’s useless. Even if you don’t care for redirection, you have to fill this. Also, it won’t work without a trailing slash. It’s quite straightforward to fill the rest.{App ID} &client_secret={App Secret}&redirect_uri={Site URL}&response_type=token &scope=read_stream,publish_stream,publish_actions,manage_pages,email,user_checkins&code={user’s single-use auth code}

After Step 5 you will get the token id.

Notes: at Step 5, specify your site with trailing slash such as “”, else you will get an error. And also, the website need to be specified within FB app suc as Site URL.

Prev PostShortening URL with and PHP
Next PostText effect - circular

Leave a reply