Auto Post on Facebook Using PHP SDK v5

This tutorial is about, how you can auto post on Facebook using PHP SDK v5. To auto post on Facebook you need a Facebook app with App ID and App secret. Also you need an Access Token of the user and Facebook PHP SDk v5.

Create Facebook APP:
To create a Facebook App please follow the steps of my previous post How to Auto Post on Facebook Using PHP and get the App ID and App secret key.

Get Facebook Access Token:
Now to get Facebook Access Token you need to use a PHP SDK (v5.0) to communicate with Facebook API. You can download PHP SDK 5.0 from following link Download Facebook PHP SDK v5.0.

Now follow my previous tutorial about Get Facebook Access Token Using PHP SDK v5 to get Access Token. You can store this Access Token to use it in future.

Auto Post on Facebook:
Now create a file autopost.php and write following code in it.

require_once __DIR__ . '/FacebookSDK/src/Facebook/autoload.php';

$fbData = array(
	'consumer_key' => APP_ID,
	'consumer_secret' => APP_SECRET,
	'default_graph_version' => 'v2.2'

$fb = new Facebook\Facebook($fbData);

//// get following data from the db or just replace them ////
$params["message"] = '{title}';
$params["link"] = '{targetUrl}';
$params["picture"] = '{imgUrl}';
$params["description"] = '{description}';

// get access token from the database or you can directly put here
$access_token = '{AccessToken}';

// post to Facebook
try {
  // Returns a `Facebook\FacebookResponse` object
  $response = $fb->post('/me/feed', $params, $access_token);
} 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();

$graphNode = $response->getGraphNode();

echo 'Posted with id: ' . $graphNode['id'];

When I posted a post with following data

$title = "Auto Post on Facebook Using PHP SDK v5";
$targetUrl = "";
$imgUrl = "";
$description= "This tutorial will help you to auto post on Facebook using PHP. For this you need FB app with App ID, App secret, PHP SDK 5.0 and access token of the user.";

I got following as a result.

an auto post example
An auto post example

This way you can auto post on Facebook timeline of the user. You can use this script with cron also. Hope this tutorial will help you.

  • Izhari Ishak Aksa


    Thanks for great tutorial. Currently I’m looking a way to post as page for my facebook page. It hard to follow the docs on facebook developer. Could you please explain about it in next tutorial?

    Thanks in advance ๐Ÿ™‚

  • Leonidas Savvides

    ‘/me/feed’ posted success
    ‘/’.$ID.’/feed’ to post like here in my Facebook Page ===> have to – EVEN TEST MODE – Request Facebook manually approve theses three permissions: manage_pages,publish_actions,publish_pages … ??? well where latter needed: and to test and to production????

  • Leonidas Savvides

    I want cron PHP script POST to MY PAGES… how I
    can provide Facebook multiple images PHP – in array? how?

    $fb->post(‘/me/feed’, $params, $access_token);
    // v.2.4 I HAVE TO USE /feed IN THIS???? FOR MULTIPLE IMGs

  • Nilamadhab Khanda

    hi , i am getting this error

    Fatal error: Uncaught exception ‘FacebookExceptionsFacebookSDKException’ with message ‘Required “app_id” key not supplied in config and could not find fallback environment variable “FACEBOOK_APP_ID”‘ in /home/sourcecc/public_html/facebook/Facebook/src/Facebook/Facebook.php:133 Stack trace: #0 /home/sourcecc/public_html/facebook/getAccessToken.php(13): FacebookFacebook->__construct(Array) #1 {main} thrown in/home/sourcecc/public_html/facebook/Facebook/src/Facebook/Facebook.php on line 133

  • Md Saifur Rahman


    Thanks for your tutorial. But I am getting following error when i tried to auto post on my timeline
    “Graph returned an error: (#200) The user hasn’t authorized the application to perform this action”

    Can you please reply me with solution? I would be grateful.