Press enter to see results or esc to cancel.

How to publish status on Facebook Pages using Graph API


Hi, In this article I'm show how to publish status on  Facebook Personal Profile, Fan Pages or Business Pages using Facebook Graph API. 

Create an Facebook App

Sign in to your Facebook Developers account and create a Facebook App. click the ‘Add a New App’ as shown below:



Enter "App Name" ckick "Create App ID". 



 Now Your dashbord will be display.




Create Non-expiring Access Tocken

You need to get  non-expiring Access Token for post content to your Facebook page automatically.
Click "Tools & support" and go to "Graph API Explorer".




Select the application in "Graph API Explorer".




Select "Get User Access Token"



Checked the permission ‘publish_pages’ and ‘manage_pages’  amd click Get Access Token




In this click ok.

Now you will get the  Access Token



You now have your Access Token which we need to convert into the non-expiring Access Token. To do so, pass your App ID, App Secret and Access Token into the following URL:

https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=appid&client_secret=appsecret&fb_exchange_token=accesstoken

Now copy the token provided.

Install the Facebook PHP SDK

At first you need to download the Facebook SDK.

Unzip the folder "Facebook". Then include the SDK in your index.php file:


define('FACEBOOK_SDK_V4_SRC_DIR', __DIR__.'/Facebook/');
require_once(__DIR__.'/Facebook/autoload.php');

Enter your ‘App ID’ and ‘App secret’ keys into the code below.

$fb = new FacebookFacebook([ 'app_id' => 'xxxxxxxxxx', 'app_secret' => 'xxxxxxxxxx', 'default_graph_version' => 'v2.2', ]);

The code for posts the content to the Facebook page as follow

<?
define('FACEBOOK_SDK_V4_SRC_DIR', __DIR__.'/Facebook/');
require_once(__DIR__.'/Facebook/autoload.php');

$fb = new FacebookFacebook([
  'app_id' => 'XXXXXX',
  'app_secret' => 'XXXXXX',
  'default_graph_version' => 'v2.2',
  ]);
 
$linkData = [
  'link' => 'http://www.tricing.com',
  'message' => 'Welcome to Tricing Blog',
  ];
$pageAccessToken="XXXXXXX";
try {
  // Returns a `FacebookFacebookResponse` object
  $response = $fb->post('/me/feed', $linkData, $pageAccessToken);
} catch(FacebookExceptionsFacebookResponseException $e) {
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(FacebookExceptionsFacebookSDKException $e) {
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}
$graphNode = $response->getGraphNode();
print_r($graphNode);

?>

 if you now run this file it will post your content to your Facebook page.

Note: You only see the post appear on the Facebook page, until you have not set your App ‘live’! Go to your Facebook Developers account and select your App. Under the ‘Status & Review’ tab tick the checkbox at the top of the page next to the text ‘Do you want to make this app and all its live features available to the general public?’.



Download Sample / Source Code

Download