As you know that Facebook introduced new Graph API v3.1 on July 26, 2018 that changed many things for developers. One of the questions I have now is how to share/post on Facebook profile/page timeline using Facebook Graph API v3.1 PHP SDK?
Reminder: Graph API v2.7 will be deprecated on Oct 05, 2018. Please use the API Upgrade Tool to understand how this might impact your app. For more details see the changelog
For this I created a new apps with some settings as shown in the below screenshots.
For this purpose, I used the below mentioned code along with facebook-php-graph-sdk-5.x.
fbConfig.php
<?php
if(!session_id()){
session_start();
}
// Include the autoloader provided in the SDK
require_once __DIR__ . '/facebook-php-graph-sdk-5.x/autoload.php';
// Include required libraries
use Facebook\Facebook;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;
/*
* Configuration and setup Facebook SDK
*/
$appId = 'APP_ID'; //Facebook App ID
$appSecret = 'APP_SECRET'; //Facebook App Secret
$redirectURL = 'MAIN_PAGE_URL_SAME_AS_IN_APPS_SETTING'; //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;
}
?>
index.php
<?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 stackoverflow.com website';
$title = 'Post From Website';
$link = 'http://www.stackoverflow.com/';
$description = 'stackoverflow is simply awesome.';
$picture = 'https://i.stack.imgur.com/MybMA.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="https://www.freeiconspng.com/uploads/facebook-login-button-png-11.png" /></a>';
}
?>
Where as my files level are as shown in the below screenshot.
Finally after doing all of the above setting and codes, when I try to run my page then I got LOGIN FACEBOOK BUTTON and after clicking there, I got the below screenshot error.
What I want is simple post my desired content without showing any POPUP or dialog so that I can easily use it via my PHP Codes.
I tried to find any working solution over internet but all are now old, nothing is now working with new Facebook Graph API v3.1.
Yes, you should post on page feed instead of the personal feed. As luschn said, you need
manage_pages
andpublish_pages
permission to obtain the correct page's access token.First, you have to develop your app in dev mode, then you need to submit for App Review with those permissions.
This process requires you to upload screencasts with a demo and put the app in a public place so that the Facebook Review team can reach it and test it.
It's a long way to go..
That is not possible anymore for user profiles. You have to use Sharing Dialogs instead.
publish_actions
got removed and there is no replacement or workaround.For Pages, you can use
manage_pages
andpublish_pages
to post to a Page with a Page Token.