Post into Facebook Page using PHP SDK v5

Last updated on by , 20 comments

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 do 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 an init.php file and paste below code into it, and replace APP_ID & APP_SECRET with your’s one (see 1.3 & 1.4).



$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 an index.php file, and paste below codes into it.


$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.


$helper = $fb->getRedirectLoginHelper();

try {

  $accessToken = $helper->getAccessToken();

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

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

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


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

try {

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

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

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


echo "<pre>";
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 a 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 is individual access_token & id (PageID) available for each page. 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


$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 the 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

20 responses 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

  2. Aubrey says:

    This “solution using PHP” is a re-hash of what most developers already know unfortunately.
    I have a site that has multiple contributors and most of them are not very “tech savvy” to put it kindly.
    The site has a “Page” on FB and I need to post a share to THAT FB page automatically each time an article is published on the site by any contributor.
    The FB Page has been set up so that only the owner (me) can post to it to prevent all sorts of rubbish being posted and shared on the page.
    This solution DOES use PHP to do the posting BUT requires all sorts of user input which in my case is unfortunately a recipe for disaster.
    How would I go about doing that?
    BTW, this article was definitely one of the better ones for doing a user based share to Facebook – thanks.

    • Hello @Aubrey,

      I think this is the article you’re searching for, I mean you just wanna automatic post to your FB Page when an article gets published. This article do perform such kinda operation.

      you just have to make some logics, i.e. setup this PHP script to gets automatically executed when an article gets published.

      If you find any problem, please feel free ask

      Thank you

  3. Manos says:

    ‘Testing Post for our new tutorial. Graph API.’);
    $res = $fb->post(‘777777/feed/’, $arr, ‘trtrtrtrtrt’);
    Doesn’t work!
    Any help please…

  4. Chris says:

    Great Article! Nice work!

  5. david says:

    Hi Paritosh,

    I am getting this error using this tut:

    “Graph returned an error: Error validating verification code. Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request”

    Any idea how I can fix it? Thanks for the help!

  6. Ismail says:

    I am getting an error “Graph returned an error: Can not load URL: This URL domain is not included in application domains. To be able to upload this URL, please add all domains and subdomains of your app to the “Application domains” field in your app settings”

    I am running your code on my localhost.
    I have “http://localhost:80/facebook”
    in settings -> Basic -> App domains

    and I have the same “http://localhost:80/facebook” in Site Url.

    I have these urls “http://localhost:80/facebook/fb-callback.php” and “http://localhost:80/facebook/” in “Valid OAuth redirect URIs” under Products -> Facebook Login.

  7. thiru says:

    Fatal error: Uncaught exception ‘Facebook\Exceptions\FacebookAuthenticationException’ with message ‘Invalid OAuth access token.’ in /opt/lampp/htdocs/fb/Facebook/Exceptions/FacebookResponseException.php:106 Stack trace: #0 /opt/lampp/htdocs/fb/Facebook/FacebookResponse.php(210): Facebook\Exceptions\FacebookResponseException::create(Object(Facebook\FacebookResponse)) #1 /opt/lampp/htdocs/fb/Facebook/FacebookResponse.php(255): Facebook\FacebookResponse->makeException() #2 /opt/lampp/htdocs/fb/Facebook/FacebookResponse.php(82): Facebook\FacebookResponse->decodeBody() #3 /opt/lampp/htdocs/fb/Facebook/FacebookClient.php(225): Facebook\FacebookResponse->__construct(Object(Facebook\FacebookRequest), ‘{“error”:{“mess…’, 400, Array) #4 /opt/lampp/htdocs/fb/Facebook/Facebook.php(469): Facebook\FacebookClient->sendRequest(Object(Facebook\FacebookRequest)) #5 /opt/lampp/htdocs/fb/Facebook/Facebook.php(367): Facebook\Facebook->sendRequest(‘POST’, ‘145081993833901…’, Array, ‘ EAAD7wcKk6c8BA…’, NULL, NULL) #6 /opt/lampp/htdocs/fb/ in /opt/lampp/htdocs/fb/Facebook/Exceptions/FacebookResponseException.php on line 106

  8. weam osama says:

    it return empty array

  9. Stra Ioannis says:

    No OAuth data could be obtained from the signed request. User has not authorized your app yet.

    Thats my message on fb-callback.php page

    • It is because user has not authorized your facebook app yet.

      see 3.1 to authorize app, you have to click on Log in with Facebook!

      I’m pretty sure you’d have done this before every application that kinda connected with facebook use this authorization step.

  10. Dan says:

    How can i add an image to my post?

Leave a Reply

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