Post to Facebook Wall from Website using PHP

Facebook is the most popular social media and shares on the Facebook wall are the most used activity by its user. Facebook share option is a common and required feature for every web application. We can easily share the post to Facebook by manually or from the script. In this tutorial, you’ll learn how to post activity on Facebook wall from website using PHP and Facebook API.

Post to Facebook wall is useful when you want to post dynamic content to Facebook from the website. Here we’ll build a simple PHP script to publish Facebook post from website using Facebook PHP SDK. This functionality lets the user submit post (message, picture, link, text content) on their Facebook timeline from website using PHP SDK v5.0 and Facebook Graph API.

Before getting started to post to Facebook wall using PHP, take a look at the files and folders structure.


Facebook Apps Creation

To access Facebook API, App ID & App Secret need to be specified on Facebook API call. You need to create a Facebook App for generating App ID & App Secret. If you’re not already created a Facebook app, visit the link below to create and configure a Facebook App from the App Dashboard.

After completing the Facebook App creation and configuration you’ll get the App ID and App secret. Copy this App ID and App Secret of your Facebook App for later use.

Facebook SDK for PHP v5.0

All Facebook PHP SDK files are included in the facebook-php-sdk/ directory, place the facebook-php-sdk/ folder into the root directory. You don’t need to download it separately, Facebook SDK v5 is included in our source code.

Facebook API Configuration (fbConfig.php)

The fbConfig.php file is used to configure Facebook SDK and connect to Facebook Graph API. Specify your Facebook App ID ($appId), App Secret ($appSecret), Callback URL ($redirectURL), and Permissions ($fbPermissions) to connect with Facebook API and working with SDK.

Note that: The access token must have the publish_actions permission to post on Facebook wall.


// Include the autoloader provided in the SDK
require_once __DIR__ '/facebook-php-sdk/autoload.php';

// Include required libraries
use Facebook\Facebook;

 * Configuration and setup Facebook SDK
$appId         'InsertAppID'//Facebook App ID
$appSecret     'InsertAppSecret'//Facebook App Secret
$redirectURL   'http://localhost/post_to_facebook_from_website/'//Callback URL
$fbPermissions = array('publish_actions'); //Facebook permission

$fb = new Facebook(array(
'app_id' => $appId,
'app_secret' => $appSecret,
'default_graph_version' => 'v2.2',

// Get redirect login helper
$helper $fb->getRedirectLoginHelper();

// Try to get access token
try {
$accessToken $_SESSION['facebook_access_token'];
$accessToken $helper->getAccessToken();
} catch(
FacebookResponseException $e) {
'Graph returned an error: ' $e->getMessage();
} catch(
FacebookSDKException $e) {
'Facebook SDK returned an error: ' $e->getMessage();

Note that: You’ll find the App ID and App Secret on your Facebook Apps settings page.

Submit Post to Facebook Wall (index.php)

Include the fbConfig.php file to connect Facebook API and get the access token.

If FB access token ($accessToken) is available, the following will happen.

  • Access token will be stored in the session that will be used for next time API calls.
  • Post message, name, link, description, and the picture will be submitted to Facebook wall.
  • Post submission status will be shown.

If FB access token ($accessToken) is not available, the Facebook Login URL will be generated and the user would be redirected to the FB login page.

//Include FB config file
require_once 'fbConfig.php';

// Put short-lived access token in session
$_SESSION['facebook_access_token'] = (string) $accessToken;
// OAuth 2.0 client handler helps to manage access tokens
$oAuth2Client $fb->getOAuth2Client();
// Exchanges a short-lived access token for a long-lived one
$longLivedAccessToken $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
// Set default access token to be used in script
//FB post content
$message 'Test message from website';
$title 'Post From Website';
$link '';
$description 'semicolonworld is a programming blog.';
$picture '';
$attachment = array(
'message' => $message,
'name' => $title,
'link' => $link,
'description' => $description,
//Post to Facebook
//Display post submission status
echo 'The post was submitted successfully to Facebook timeline.';
FacebookResponseException $e){
'Graph returned an error: ' $e->getMessage();
FacebookSDKException $e){
'Facebook SDK returned an error: ' $e->getMessage();
//Get FB login URL
$fbLoginURL $helper->getLoginUrl($redirectURL$fbPermissions);
//Redirect to FB login

Related Articles

Comments 0