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 (4) - Integrate with Opencart

In the last part of this article, I am going to show you a simple example how to integrate the Facebook PHP SDK and Graph API into Opencart and auto publish products to Facebook Page. The example is a simplified version of my own script. You might want to modify this script on your shopping cart.

This script will randomly pick one of the published product from Opencart MySQL database and use the code in previous article for auto posting. By running the script under crob tab four times a day, for example, there are four product published everyday. 

This script is tested on Opencart 1.5.6.4.

Here is the sample code to do that:

 

** 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;

// include configuration file from opencart
include 'config.php';
	$caption = '< Webstore Slogan >';
	$domain = '< Your website domain >';
	$image_dir = $domain . 'image/';
	
	$link = mysqli_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
	$sql = "SELECT MAX(product_id) FROM " . DB_PREFIX . "product";
	$result= $link->query($sql);
	$max=$result->fetch_row();	
    $random_id = rand(50, $max[0]);

	$sql = "SELECT DISTINCT *, (SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'product_id=" . $random_id . "') AS keyword FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) WHERE p.product_id = '" . $random_id  . "'";

	$result = $link->query($sql);
	$product = $result->fetch_assoc();

	// 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' => $product[name],
		'link' => $domain . $product[keyword],
		'picture' => $image_dir . $product[image],
		'caption' => $caption,
		'message' => $product[name] . " - See more at: " . $domain . $product[keyword],
  ) ))->execute()->getGraphObject()->asArray();
 
?>

 

Assuming we place the script in the main directory of the domain. We include Opencart configuration file which has the setup information of MySQL.

include 'config.php';

We then define $caption to store slogan, or any tag line, as well as domain name and path to image directory.

	$caption = '< Webstore Slogan >';
	$domain = '< Your website domain >';
	$image_dir = $domain . 'image/';

 Now connect to MySQL database and find the max value of product_id used. If we increase the product quantity in future, this number will increase as well.

	$link = mysqli_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
	$sql = "SELECT MAX(product_id) FROM " . DB_PREFIX . "product";
	$result= $link->query($sql);
	$max=$result->fetch_row();	

 You can get the min value of product_id used as well. In this case, I just hardcode to 50 as my database started with this number. We generate a random product id for auto publish.

$random_id = rand(50, $max[0]);

 The query below will retrieve all product info with id = $random_id from three table in MySQL : _product, _url_alias and _product_description. If you want to know content of the output, just do print_r($product).

	$sql = "SELECT DISTINCT *, (SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'product_id=" . $random_id . "') AS keyword FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) WHERE p.product_id = '" . $random_id  . "'";

	$result = $link->query($sql);
	$product = $result->fetch_assoc();

 Now, just modify the autoposting on Facebook Graph API.

	$page_post = (new FacebookRequest( $session, 'POST', '/'. $page_id .'/feed', array(
		'access_token' => $access_token,
		'name' => $product[name],
		'link' => $domain . $product[keyword],
		'picture' => $image_dir . $product[image],
		'caption' => $caption,
		'message' => $product[name] . " - See more at: " . $domain . $product[keyword],
  ) ))->execute()->getGraphObject()->asArray();

 This script publishes one product to Facebook page per execution. Schedule this script to run multiple time in cron job to publish multiple products. Now we can sit down and relax!

 

 

 

 

 

 

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

20 comments

  • Abhishek Soni

    hello,
    great script, it worked via browser but when i added it to cron job its not posting updates to the page .

    could you let me know the corn command or the reason why this is happening ?

    do you tried adding this to cron job ?

    thanks

    posted by Abhishek Soni Sunday, 11 January 2015 09:01 Comment Link
  • Chin-Hock Tan

    Hello Abhishek,
    The script is working. This one run auto posting every 4 hours.
    http://facebook.com/pages/Wagga-Shop/207278019334554

    For debug, you can place your script under public_html and set your cron job command to
    /usr/bin/wget -O /dev/null -T 0 http://domain name/script name.php 1> /dev/null 2> /dev/null

    posted by Chin-Hock Tan Sunday, 11 January 2015 23:55 Comment Link
  • Abhishek Soni

    hey ,
    yes its working fine actually i had a syntax error because of this the cron job wasnt working .

    anyway , i have one more question.
    if i want to post a picture to Facebook instead of link , then what would be the code to do so ?

    Thanks a lot :)

    posted by Abhishek Soni Monday, 12 January 2015 08:05 Comment Link
  • Chin-Hock Tan

    Hello,
    If I understand your question correctly, you can just remove the $link. Then there is no link to any website.
    Thx.

    posted by Chin-Hock Tan Monday, 12 January 2015 08:18 Comment Link
  • Abhishek Soni

    Hello,
    well i tried with removing the $link but its not working .

    i guess you didnt get my question , i want to make a photo post instead of link post sir :)

    posted by Abhishek Soni Tuesday, 13 January 2015 06:04 Comment Link
  • Chin-Hock Tan

    Hi Abhishek,
    Got your question. You need to change to /{page-id}/photos instead of "feed".

    Here is reference from facebook
    https://developers.facebook.com/docs/graph-api/reference/v2.2/page/photos

    Check the publishing section.

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

    Fatal error: Class 'Facebook\FacebookSession' not found

    why?

    posted by Arfian Hidayat Tuesday, 24 February 2015 05:55 Comment Link
  • Digital Latitude SEO

    Great article, just what I was looking for.

    posted by Digital Latitude SEO Monday, 02 March 2015 06:11 Comment Link
  • Jay

    hello, any example on virtuemart?

    posted by Jay Wednesday, 25 March 2015 07:21 Comment Link
  • Kent R.

    what about Woocommerce for wordpress?

    posted by Kent R. Thursday, 26 March 2015 01:49 Comment Link
  • Chin-Hock Tan

    Hello Jay, most likely I create one for virtuemart in future and post it here.

    posted by Chin-Hock Tan Thursday, 26 March 2015 02:01 Comment Link
  • Chin-Hock Tan

    Kent, sorry, not using Woocommerce or wordpress.

    posted by Chin-Hock Tan Thursday, 26 March 2015 02:04 Comment Link
  • Tanya M.

    My problem solved with your modified code. Thanks for your help.

    posted by Tanya M. Friday, 30 October 2015 01:27 Comment Link
  • Chin-Hock Tan

    You are welcome!

    posted by Chin-Hock Tan Friday, 30 October 2015 04:26 Comment Link
  • KY Teh

    Hi, will the code work on Opencart 2.x?

    posted by KY Teh Thursday, 16 June 2016 09:28 Comment Link
  • Chin-Hock Tan

    Hello KY,
    I have not tested on OC2.x. My code is pulling data from three MySQL tables _product, _url_alias and _product_description.
    You might need slight modification to my code if there are any changes to these tables.

    posted by Chin-Hock Tan Thursday, 16 June 2016 16:00 Comment Link
  • Cristian

    Hello!
    Great script, the single share product manual posting worked like a charm.
    I am trying the opencart script for a website working on opencart v2.0.3.1, and I get the following error:

    Fatal error: Uncaught exception 'Facebook\FacebookServerException' with message 'Service temporarily unavailable' in /home/motherwa/public_html/Facebook/FacebookRequestException.php:110 Stack trace: #0 /home/motherwa/public_html/Facebook/FacebookRequest.php(280): Facebook\FacebookRequestException::create('{"error":{"mess...', Object(stdClass), 400) #1 /home/motherwa/public_html/Facebook/facebook_page_autoposting.php(49): Facebook\FacebookRequest->execute() #2 {main} thrown in /home/motherwa/public_html/Facebook/FacebookRequestException.php on line 110

    Could you assist me with this issue?

    Keep up the great work!

    posted by Cristian Monday, 03 April 2017 20:40 Comment Link
  • Chin-Hock Tan

    Hello Cristian,
    My example was based on OpenCard 1.5 and you are using 2.x.
    Make sure your $sql has valid return values. You can do that by dump on $product.
    Good luck.

    posted by Chin-Hock Tan Tuesday, 04 April 2017 02:19 Comment Link
  • Cristian

    Hello Chin-Hock,
    Thank you for your reply!
    I used the following code to dump:
    $arr = get_defined_vars();
    print_r($arr);

    And found out I did not use a "/" after the domain, which screwed up everything.

    Thank you once again, it works now!

    posted by Cristian Tuesday, 04 April 2017 18:38 Comment Link
  • Chin-Hock Tan

    Hello Cristian,
    Glad to know that your problem solved!

    posted by Chin-Hock Tan Wednesday, 05 April 2017 16:07 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