Post into Facebook Page using PHP SDK v5

Last updated on August 19th, 2017 by Paritosh Pandey


In this article we are going to learn how you can directly post into facebook page using php from your website or through your own platform.

There are lots of companies available on the Internet which provides such kind of services i.e. Hootsuite, Sproutsocial & Socialbakers etc.

Facebook always tries to motivate developers, to build such kind of modules facebook has provided its php library for php developers called Facebook SDK v5. This SDK is a library with powerful features that enable PHP developers to easily integrate Facebook login and make requests to the Graph API.

Post into Facebook Page using PHP Module

1. Create a Facebook App

1.1 – First we need to create a facebook app in order to use their API, sign in to your Facebook Developers account and click the “Add a New App” or “Get Started“(if you not have a developers account) link.

1.2 – click on “Settings“, scroll down and you’ll see a “+ Add Platform” just click on it, now it will ask for your website url, just put your website url (url where you want this module to be hosted on) into it.

1.3 – in “Settings” you will see the App ID copy  it, we’ll need it further.

1.4 – again in “Settings” you will see the App Secret, to grab it you have to click on show button and have to enter your facebook password.

2. Installing the Facebook PHP SDK

2.1 – First we need to download the facebook php sdk and upload it on your server (website url, see 1.2), we just need to upload the Facebook directory.

2.2 – create a init.php file and paste below code into it, and replace APP_ID & APP_SECRET with your’s one (see 1.3 & 1.4).


session_start();

require_once('Facebook/autoload.php');

$fb = new Facebook\Facebook([
'app_id' => 'APP_ID',
'app_secret' => 'APP_SECRET',
'default_graph_version' => 'v2.9',
]);

3. The actual code for Post into Facebook Page using PHP

3.1 – create a index.php file, and paste below codes into it.


include('init.php');

$helper = $fb->getRedirectLoginHelper();

$permissions = ['manage_pages','publish_actions','publish_pages'];
$loginUrl = $helper->getLoginUrl('YOUR_WEBSITE_URL/fb-callback.php', $permissions);

echo '<a href="' . htmlspecialchars($loginUrl) . '">Log in with Facebook!</a>';

replace “YOUR_WEBSITE_URL” in above code (website url, see 1.2), as you can see in above code you have to create a fb-callback.php file.

In above code we have to set a callback url (fb-callback.php), facebook uses it to send back an access token. So go ahead and create a fb-callback.php file and paste below code into it.


include('init.php');

$helper = $fb->getRedirectLoginHelper();
$_SESSION['FBRLH_state']=$_GET['state'];

try {

  $accessToken = $helper->getAccessToken();

} catch(Facebook\Exceptions\FacebookResponseException $e) {

  echo 'Graph returned an error: ' . $e->getMessage();
  exit;

} catch(Facebook\Exceptions\FacebookSDKException $e) {
  
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;

}

if (! isset($accessToken)) {
  echo 'No OAuth data could be obtained from the signed request. User has not authorized your app yet.';
  exit;
}

try {

  $response = $fb->get('me/accounts', $accessToken->getValue());
  $response = $response->getDecodedBody();

} catch(Facebook\Exceptions\FacebookResponseException $e) {
  
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;

} catch(Facebook\Exceptions\FacebookSDKException $e) {
  
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;

}

echo "<pre>";
print_r($response);
echo "</pre>";

As of now when we execute our index.php file you will see a “Log in with Facebook!” link there. When you click on that link you will be get redirected to a facebook page. This page asks user to authorize our app, when they accept it they will get redirected to our fb-callback.php file, facebook returns the list of all pages administrated by that user, which is stored in “$response” variable.

post into facebook page using php

as you can see above in screenshot, there are individual access_token & id (PageID) available for each pages. Finally these two credentials are required for posting stories on correspondence facebook page.

3.2 – now create a “post.php” file, and paste below codes into it


include('init.php');

$arr = array('message' => 'Testing Post for our new tutorial. Graph API.');

$res = $fb->post('PAGE_ID/feed/', $arr,	'ACCESS_TOKEN');

replace PAGE_ID & ACCESS_TOKEN with your page’s credentials as shown in above screenshot. By running post.php file you will get something like below.

facebook php sdk

Hope you like post into facebook page using php module, please subscribe us to get latest updates and news. We are going to post lots of interesting articles in future, please like and share this article with your friends.

You may also like

One response to “Post into Facebook Page using PHP SDK v5”

  1. Anil Munde says:

    hi very good article thanks for sharing keep up the good work

Leave a Reply

Your email address will not be published. Required fields are marked *