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

Chin-Hock Tan
I am a full time internet retailer, selling physical products through my own websites and various internet marketplaces. I write PHP web bots and screen scraper scripts during my free time for email marketing to increase web traffic, scraping products from one website to another to minimize manual entries, aggregate content for new websites etc.
I am available for hire as freelance PHP coder on web bots, screen scraper and data mining. I quote fixed price for your project if the detail of requirements are clearly outlined.
I also help customers to build and host Joomla based business/content/blogging website, shopping cart with Virtuemart, Presta Shop, Open Cart, EC Shop, EC Mall etc. First year hosting is free.
I accept payment via Paypal. If you would like to contact me, please write to freeman [a] php8legs.com. TQ.
我是一名全职的网上零售商,通过自己的网站和不同的交易平台售卖实物商品。我在空闲时间写PHP机器人和网络资料提取脚本,并用于电子邮件营销以增加网站流量,从一个网站提取产品信息到另一个网站以减少手工输入,为新网站聚合内容等。
您可聘请我编码PHP机器人,网络资料提取脚本及数据挖掘。如果您详细明确阐述您的要求,我会报您一个固定的价格。
我也为客户用Joomla建立业务/内容/博客相关网站,用Virtuemart, Presta Shop, Open Cart, EC Shop, EC Mall等创建购物网站。我提供网页寄存,第一年是免费的。
我接受通过PayPal付款。如果您想联系我,请写信给“freeman [a] php8legs.com”。 谢谢。
Website: php8legs.comLatest from Chin-Hock Tan
- How to auto publish post on Facebook Fan Page as admin using Facebook PHP SDK V4 Graph API v2.2 (4) - Integrate with Opencart
- How to auto publish post on Facebook Fan Page as admin using Facebook PHP SDK V4 Graph API v2.2 (2) - Get Permanent Page Access Token
- How to auto publish post on Facebook Fan Page as admin using Facebook PHP SDK V4 Graph API v2.2 (1) - Create Facebook App
- Price Comparison Engine using SphinxSearch
- Facebook Remote Status Update with PHP/cURL Bot
Related items
- How to auto publish post on Facebook Fan Page as admin using Facebook PHP SDK V4 Graph API v2.2 (4) - Integrate with Opencart
- How to auto publish post on Facebook Fan Page as admin using Facebook PHP SDK V4 Graph API v2.2 (2) - Get Permanent Page Access Token
- How to auto publish post on Facebook Fan Page as admin using Facebook PHP SDK V4 Graph API v2.2 (1) - Create Facebook App
- Facebook Remote Status Update with PHP/cURL Bot
- USD1.60 To Put Facebook Under Your Feet!