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 on 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 the post (message, picture, link, text content) on their Facebook timeline from the 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.
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.
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.
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.6',
));
// 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.
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.
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 configuration 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 published successfully to the 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 Facebook login URL
$fbLoginURL = $helper->getLoginUrl($redirectURL, $fbPermissions);
// Redirect to Facebook login page
echo '<a href="'.$fbLoginURL.'"><img src="fb-btn.png" /></a>';
}
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.
Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request
Hi,
I love your tutorial on Post to Facebook Wall from Website using PHP.
Could you please send me the source code or inform me where to download?
Thanks.
Hi,
Where can I download the source conde for this?
Kind Regards
Dian
Sir, is there is any code to to show posts(image,video etc) in website, the posts are dynamically changing according to the Facebook recent posts. Thanks a lot Sir, Reply sir i need this for my final year project. My email address:(hazratbilal58262@gmail.com)
Hello, do i need a approved app to work the auto post in wall?
Yes, approved Facebook App with required permission is needed for this API.
Sir from where to get these files that you have included in your folder like fb_config,sdk files.
Is it possible to post to wall using graph api the latest version? I mean to post without any native dialog or share button like in your article?
Thanks!
i want to share my posts to facebook from my php site but the tittle of the page appears instead of the tittle of the post
This is a great post. Saved my day. Thanks Dude!
Very nice script thanks
Amazing tutorial. Any way to post like and checkin on a public page? Thank you
Nice tutorial
IS there any way to upload photos on user timeline from website?
Hi,
I used your code to post comments on facebook its working fine.Thank you
Is it possible to post comments on my friend facebook wall or company official page