Posting to FB pages from web application

This is the code:

$appId = 'YOUR APP ID';
$secret = 'YOUR SECRET';
$returnurl = 'http://www.yoursite.com';
$permissions = 'manage_pages, publish_stream, offline_access';

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

if($fbuser){

$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"];
break;
}
}
}
else
{
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
$fb->setAccessToken($page_access_token);

// Now try to post on page's wall
try{
$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);
if($result){
echo 'Successfully posted to Facebook Wall...';
}
}catch(FacebookApiException $e){
echo $e->getMessage();
}

}else{

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

}

of course you will need the SDK from here:
https://developers.facebook.com/docs/php/gettingstarted/

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 http://developers.facebook.com/apps 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, https://graph.facebook.com/oauth/authorize?client_id={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. https://graph.facebook.com/oauth/access_token?client_id={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 “http://www.berita.ecom.my/”, else you will get an error. And also, the website need to be specified within FB app suc as Site URL.

Leave a Reply