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:

 

Note: Check out the sample code at bottom of this article.

<?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!

 

 Code:

<?php
session_start();
include 'config.php';
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();
 
?>

 

 

 

 

 

 

Last modified on Tuesday, 29 December 2020 07:07
Rate this item
(2 votes)
back to top