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:
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; // 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.
Code:
<?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 ); ?>
Related items
- How to auto publish post on Instagram Page with PHP/cURL and without using Instagram API (1)
- New and Updated! Facebook Remote Status Update with PHP/cURL Bot
- 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