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.

post-to-facebook-wall-from-website-php-sdk-files-structure-codexworld

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.

<?php
if(!session_id()){
    
session_start();
}

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

// Include required libraries
use Facebook\Facebook;
use 
Facebook\Exceptions\FacebookResponseException;
use 
Facebook\Exceptions\FacebookSDKException;

/*
 * 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 {
    if(isset(
$_SESSION['facebook_access_token'])){
        
$accessToken $_SESSION['facebook_access_token'];
    }else{
        
$accessToken $helper->getAccessToken();
    }
} catch(
FacebookResponseException $e) {
     echo 
'Graph returned an error: ' $e->getMessage();
      exit;
} catch(
FacebookSDKException $e) {
    echo 
'Facebook SDK returned an error: ' $e->getMessage();
      exit;
}
?>

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.

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

if(isset(
$accessToken)){
    if(isset(
$_SESSION['facebook_access_token'])){
        
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
    }else{
        
// 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->setDefaultAccessToken($_SESSION['facebook_access_token']);
    }
    
    
//FB post content
    
$message 'Test message from CodexWorld.com website';
    
$title 'Post From Website';
    
$link 'http://www.codexworld.com/';
    
$description 'CodexWorld is a programming blog.';
    
$picture 'http://www.codexworld.com/wp-content/uploads/2015/12/www-codexworld-com-programming-blog.png';
            
    
$attachment = array(
        
'message' => $message,
        
'name' => $title,
        
'link' => $link,
        
'description' => $description,
        
'picture'=>$picture,
    );
    
    try{
        
//Post to Facebook
        
$fb->post('/me/feed'$attachment$accessToken);
        
        
//Display post submission status
        
echo 'The post was submitted successfully to Facebook timeline.';
    }catch(
FacebookResponseException $e){
        echo 
'Graph returned an error: ' $e->getMessage();
        exit;
    }catch(
FacebookSDKException $e){
        echo 
'Facebook SDK returned an error: ' $e->getMessage();
        exit;
    }
}else{
    
//Get FB login URL
    
$fbLoginURL $helper->getLoginUrl($redirectURL$fbPermissions);
    
    
//Redirect to FB login
    
header("Location:".$fbLoginURL);
}

Conclusion

We’ve tried to provide a simple way to share the post to Facebook wall from website using PHP. Hope! our script will help you to post on Facebook wall from your website. Also, you can change the post content dynamically by specifying the respective value in $attachment array.

Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request

Recommended Tutorials For You

3 Comments

  1. Me Said...
  2. Md Abubakar Said...
  3. Dhurga Said...

Leave a reply