Log in Register

Log in

How to auto publish post on Facebook Fan Page as admin using Facebook PHP SDK V4 Graph API v2.2 (3) - Single Product Manual Posting

Facebook Auto Posting

After setting up Facebook App and getting a permanent Page Access Token, we now write a simple code to post a product to Facebook page. We need to download facebook PHP SDK v4 from github. You can get the file from here. Unzip the file and go to directory "Facebook". You can upload this directory to your server. 

Since v4.0.9, Facebook PHP SDK v4 come with autoloader, we don't have to use Composer (Dependency Manager for PHP). For the file downloaded just now, there is a file call "autoloader.php" before "src" directory. In my case, I moved this file into Facebook directory as well. 

Here is the code:

 

** You need to Register before download this file.

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

// use Facebook/autoload.php;
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
use Facebook\FacebookRedirectLoginHelper;

// Product information
$name = '< Product Name >';
$link = '< Product URL >';
$picture = 'http://<image url >';
$caption = '< Webstore Slogan > ';
$message = '< Product Short Description or anything >';

// Facebook App related
$api_key = '< APP Id >';
$api_secret = '< APP Secret >';
$accessToken='< Facebook Page Access Token>';
$page_id = '< Facebook Page Id >';

// start a session for this App
FacebookSession::setDefaultApplication($api_key, $api_secret);
$session = new FacebookSession($accessToken);

// Auto posting
$page_post = (new FacebookRequest( $session, 'POST', '/'. $page_id .'/feed', array(
    'access_token' => $access_token,
    'name' => $name,
    'link' => $link,
	'picture' => $image,
    'caption' => $caption,
    'message' => $message,
  ) ))->execute()->getGraphObject()->asArray();

// return post_id, optional
print_r( $page_post );

?>

 

The code is quite simple. First, we need to define the path to facebook php sdk files, as well as path to autoloader.php.

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

Because autoloader is used, we can just use the keyword "use" to import the namespace and necessary files automatically.

use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
use Facebook\FacebookRedirectLoginHelper;

These are product information that going for facebook page posting. The next step is to retrieve product information from database. For now, we just supply the information manually.

$name = '< Product Name >';
$link = '< Product URL >';
$picture = 'http://<image url >';
$caption = '< Webstore Slogan > ';
$message = '< Product Short Description or anything >';

These are Facebook App information created from last two articles.

$api_key = '< APP Id >';
$api_secret = '< APP Secret >';
$accessToken='< Facebook Page Access Token>';
$page_id = '< Facebook Page Id >';

Setting session using Facebook App ID, Secret and permanent Page Access Token..

FacebookSession::setDefaultApplication($api_key, $api_secret);
$session = new FacebookSession($accessToken);

This is the code that put everything together and post on facebook page remotely.

$page_post = (new FacebookRequest( $session, 'POST', '/'. $page_id .'/feed', array(
    'access_token' => $access_token,
    'name' => $name,
    'link' => $link,
	'picture' => $image,
    'caption' => $caption,
    'message' => $message,
  ) ))->execute()->getGraphObject()->asArray();

Here is the sample output seen by your followers:

Auto posting result 

The product description is scraped by Facebook and displayed at the line between $name and $caption. When users click at the picture or message, facebook will redirect them to your product detail page specified by $link.

Next is to integrate this piece of code with Opencart database and run under daily cron job for autoposting. 

Last modified on Thursday, 03 November 2016 15:10
Rate this item
(2 votes)

22 comments

  • Arfian Hidayat

    Fatal error: Class 'Facebook\FacebookSession' not found in /home/lalamila/public_html/fbpost.php on line 27

    why? please help me

    posted by Arfian Hidayat Tuesday, 24 February 2015 05:59 Comment Link
  • Arfian Hidayat

    Fatal error: Class 'Facebook\FacebookSession' not found in /home/lalamila/public_html/fbpost.php on line 27

    why? please help me

    posted by Arfian Hidayat Tuesday, 24 February 2015 05:59 Comment Link
  • Chin-Hock Tan

    Arfian,

    Can you add
    require_once( ‘Facebook/FacebookSession.php’ );
    before the line
    use Facebook\FacebookSession;

    This could be your autoload.php setup issue.

    posted by Chin-Hock Tan Thursday, 26 February 2015 06:59 Comment Link
  • Arfian Hidayat

    I am success, thank you

    then, i want to post to groups where i follow, how to? help me, please

    posted by Arfian Hidayat Friday, 06 March 2015 13:34 Comment Link
  • Chin-Hock Tan

    Arfian, glad to know that you are able to post.

    posted by Chin-Hock Tan Saturday, 07 March 2015 02:49 Comment Link
  • Arfian Hidayat

    How to auto publish on facebook group as member group?
    can you help me, please

    posted by Arfian Hidayat Saturday, 07 March 2015 13:00 Comment Link
  • Chin-Hock Tan

    Hello Arfian,
    I had not tried to auto post to Facebook group.
    However, you can try out the code from this web page. http://www.phpgang.com/how-to-post-into-facebook-group-with-php-using-graph-api_728.html

    Good luck.

    posted by Chin-Hock Tan Sunday, 08 March 2015 02:26 Comment Link
  • Arfian Hidayat

    Mr,
    I am success post to fans page, but just me can see this post, why?
    another people cannot see this post in my fans page

    posted by Arfian Hidayat Sunday, 15 March 2015 19:07 Comment Link
  • younhoon

    Hello. Chin-Hock Tan
    Thank for your post about Facebook SDK.
    I tried to make my app like yours.
    I've got permanent access token, app id, app secret and app id. I put everything in the php file. but Nothing happened...
    Could plz check my source? How can i show u?

    posted by younhoon Monday, 16 March 2015 11:11 Comment Link
  • younhoon

    Hello. Chin-Hock Tan
    Thank for your post about Facebook SDK.
    I tried to make my app like yours.
    I've got permanent access token, app id, app secret and app id. I put everything in the php file. but Nothing happened...
    Could plz check my source? How can i show u?

    posted by younhoon Monday, 16 March 2015 11:12 Comment Link
  • younhoon

    Hello. Chin-Hock Tan
    I tired this script on my page. and i works.
    i have a question. i managed few pages.
    so, i need page list and posing by selected.
    for example, i choice A page, and post A msg.
    sometime post A msg to B,C,D pages. is it possible?

    posted by younhoon Monday, 16 March 2015 21:07 Comment Link
  • Chin-Hock Tan

    Hello Arfian,
    You might want to check your facebook page setup, as well as setup for individual post.
    The script in my example does not block any display to public.

    posted by Chin-Hock Tan Tuesday, 17 March 2015 05:39 Comment Link
  • Chin-Hock Tan

    Hello Younhoon,
    If I understand correctly, you are now able to post items to facebook page.
    Now, you want selective items posted to facebook page A, and / or posted to facebook page B, C or D...

    $page_post = (new FacebookRequest( $session, 'POST', '/'. $page_id .'/feed', array(
    'access_token' => $access_token,

    You can change the $page_id when you want to switch facebook page for posting.

    posted by Chin-Hock Tan Tuesday, 17 March 2015 05:50 Comment Link
  • younhoon

    thank you for answer.
    I want to get all pages list managed by me.
    so i put this code. but i doesn't work.

    FacebookSession::setDefaultApplication($api_key, $api_secret);
    $session = new FacebookSession();


    $getPages = (new FacebookRequest(
    $session,
    'GET',
    '/me/accounts'
    ))->execute()->getGraphObject()->asArray();

    print_r($getpages);

    }

    can u help me?

    posted by younhoon Tuesday, 17 March 2015 21:16 Comment Link
  • Chin-Hock Tan

    Hello Younhoon,
    You still need to use access token in $session = new FacebookSession().
    and syntax print_r($getPages); (upper case P).

    You can test your query by going to Facebook Developer API Explorer
    https://developers.facebook.com/tools/explorer/?method=GET&path=me%3Ffields%3Did%2Cname&version=v2.2

    You can test your query before put into code.

    posted by Chin-Hock Tan Wednesday, 18 March 2015 13:47 Comment Link
  • Tolo

    Hi there Chin-Hock Tan, first of all I want to congratulate you for this great tutorial! I've been looking around many hours looking for just one full example of auto-posting with sdk V4 and yours is the best!
    Nevertheless I have a problem that you maybe could shed some light on! Your code is working fine for me but not always! Isn't it strange? With the same code sometimes is working but sometimes I get a 500 Internal Server Error. Is needed any time between posts? I don't know what to think... If you could give some hints that would be very useful!
    Thanks in advance!

    posted by Tolo Friday, 20 March 2015 17:52 Comment Link
  • Chin-Hock Tan

    Hello Tolo,
    Thanks for the feedback.
    The code works fine for me. I did not see any server error on my website. I set cron job for the code to autopost every 4 hours per day and still running fine. No ideas on the problem you are facing.

    posted by Chin-Hock Tan Saturday, 21 March 2015 02:43 Comment Link
  • budi

    i was select permission to manage_page and publish_actions, can you help me?

    Fatal error: Uncaught exception 'Facebook\FacebookPermissionException' with message '(#200) The user hasn't authorized the application to perform this action' in C:\xampp\htdocs\bot\Facebook\FacebookRequestException.php:128 Stack trace: #0 C:\xampp\htdocs\bot\Facebook\FacebookRequest.php(280): Facebook\FacebookRequestException::create('{"error":{"mess...', Object(stdClass), 403) #1 C:\xampp\htdocs\bot\post.php(38): Facebook\FacebookRequest->execute() #2 {main} thrown in C:\xampp\htdocs\bot\Facebook\FacebookRequestException.php on line 128

    posted by budi Saturday, 30 May 2015 00:16 Comment Link
  • budi

    i was select permission to manage_page and publish_actions, can you help me?

    Fatal error: Uncaught exception 'Facebook\FacebookPermissionException' with message '(#200) The user hasn't authorized the application to perform this action' in C:\xampp\htdocs\bot\Facebook\FacebookRequestException.php:128 Stack trace: #0 C:\xampp\htdocs\bot\Facebook\FacebookRequest.php(280): Facebook\FacebookRequestException::create('{"error":{"mess...', Object(stdClass), 403) #1 C:\xampp\htdocs\bot\post.php(38): Facebook\FacebookRequest->execute() #2 {main} thrown in C:\xampp\htdocs\bot\Facebook\FacebookRequestException.php on line 128

    posted by budi Saturday, 30 May 2015 00:17 Comment Link
  • Chin-Hock Tan

    Hello Budi,
    Can you try on a live server, instead of using xampp on your desktop?

    This is because when you create the Facebook app, you are required to enter the associated website name. The Facebook ID generated that you use in the code is for live website.

    posted by Chin-Hock Tan Monday, 01 June 2015 04:39 Comment Link
  • Domenico

    Work perfetct! Thank you!
    P.S.: the first message (from $message) and second message are be different like when someone paste a link on the wall?

    posted by Domenico Tuesday, 02 June 2015 22:11 Comment Link
  • Chin-Hock Tan

    Hello Domenico,
    Glad to know the code works for you.
    Not sure what you mean by "second message"?

    posted by Chin-Hock Tan Wednesday, 03 June 2015 20:57 Comment Link

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.



Anti-spam: complete the task
back to top