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 using the HTML button. 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 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 and Facebook API.

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

post-to-facebook-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 PHP SDK

All Facebook PHP SDK files are included in the inc/ directory, place the inc/ folder into the root directory. You can get all Facebook PHP SDK files in our source file ZIP.

Facebook API Configuration (fbConfig.php)

The fbConfig.php file is used to configure and call Facebook API. Specify your Facebook App ID ($appId) and App Secret ($appSecret) to connect FB API.

<?php
//Include Facebook SDK
require_once 'inc/facebook.php';

/*
 * Configuration and setup FB API
 */
$appId 'InsertAppID'//Facebook App ID
$appSecret 'InsertAppSecret'// Facebook App Secret

//Call Facebook API
$facebook = new Facebook(array(
    
'appId'  => $appId,
    
'secret' => $appSecret
));
?>

Submit Post to Facebook Wall (index.php)

Include the fbConfig.php file to connect Facebook API and get FB user ID.

If FB user ID is available, the following will happen.

  • Determines the access token that should be used for API calls.
  • Post message, name, link, description, and the picture will be submitted to Facebook wall.
  • Post submission status will be shown.

If FB user ID 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';

//Get FB user
$fbUser $facebook->getUser();
        
if(
$fbUser){ 
    
$accessToken $facebook->getAccessToken();
    
    
//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(
        
'access_token' => $accessToken,
        
'message' => $message,
        
'name' => $title,
        
'link' => $link,
        
'description' => $description,
        
'picture'=>$picture,
    );
    
    try{
        
//Post to Facebook
        
$facebook->api("/me/feed""post"$attachment);

        
//Display post submission status
        
echo 'The post was submitted successfully to Facebook timeline.';

    }catch(
FacebookApiException $e){
        echo 
$e;
    }
}else{
    
//Get FB login URL
    
$redirectURL 'http://localhost/nitya/facebook_post_from_website/';
    
$fbLoginURL $facebook->getLoginUrl(
        array(
            
'redirect_uri' => $redirectURL,
            
'scope' => 'publish_actions'
        
)
    );
    
    
//Redirect to FB login
    
header("Location:".$fbLoginURL);
}
?>

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

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.

Recommended Tutorials For You

Leave a reply

CONNECT WITH CODEXWORLD