How to publish status on Facebook Pages using Graph API
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 Token
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’ and 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:
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' => 'https://www.techdasher.com', 'message' => 'Welcome to Techdasher 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?’.
0 Comment