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.


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.

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

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

//Get FB user
$fbUser $facebook->getUser();
$accessToken $facebook->getAccessToken();
//FB post content
$message 'Test message from website';
$title 'Post From Website';
$link '';
$description 'CodexWorld is a programming blog.';
$picture '';
$attachment = array(
'access_token' => $accessToken,
'message' => $message,
'name' => $title,
'link' => $link,
'description' => $description,
//Post to Facebook

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

FacebookApiException $e){
//Get FB login URL
$redirectURL 'http://localhost/nitya/facebook_post_from_website/';
$fbLoginURL $facebook->getLoginUrl(
'redirect_uri' => $redirectURL,
'scope' => 'publish_actions'
//Redirect to FB login

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


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