Post into facebook page using graph API with PHP

Abhigyan Singh 08th Oct 2020

Facebook page is the most popular social media platform in the world. Facebook provides various open-source API for developing many kinds of apps, games, social services functionality. I have used Facebook's own language named Facebook Query Language.

To understand the below tutorial, you must have knowledge of Facebook application development and its API. Just follow the bellows step to creating Facebook graph API.

Step: 1

Firstly I’ll create a PHP file.  My created config.php file for store variables, Facebook App ID, secret ID, return URL, etc.

My config.php file contains the below code.

<?php
include_once("include/facebook.php"); //include facebook SDK

######### edit details ##########
$app_id = 'YOUR_APP_ID'; //Facebook App ID
$app_secret = 'YPUR_APP_SECRET'; // Facebook App Secret
$return_url = 'https://localhost/publish_to_wall/process.php'; //return url (url to script)
$home_url = 'https://localhost/publish_to_wall/'; //return to home
$fb_permissions = 'publish_stream,manage_pages'; //Required facebook permissions
##################################

//Call Facebook API
$facebook = new Facebook(array(
'app_id' => $app_id,
'secret' => $app_secret
));

$fbuser = $facebook->getUser();
?>

Nice, config.php file is done for stores facebook application information.

Step: 2

Now I’ll create a PHP file for display front page. I have created an index.php file for contains a message from for this demo & user authentication page. Apps send a request to the user for authentication information.

My index.php files code is given below.

<?php
include_once("config.php");
if ($fbuser) {
try {
$user_profile = $facebook->api('/me');
//Get user pages details using Facebook Query Language (FQL)
$fql_query = 'SELECT page_id, name, page_url FROM page WHERE page_id IN (SELECT page_id FROM page_admin WHERE uid='.$fbuser.')';
$post_results = $facebook->api(array( 'method' => 'fql.query', 'query' => $fql_query ));
} catch (facebook_api_exception $e) {
echo $e->getMessage();
$fbuser = null;
}
}else{
//Show login button for guest users
$login_url = $facebook->get_login_url(array('redirect_uri'=>$home_url,'scope'=>$fb_permissions));
echo '<a href="'.$login_url.'"><img src="img/facebook-login.png" border="0"></a>';
$fbuser = null;
}

if($fbuser && empty($post_results))
{
/*
if user is logged in but FQL is not returning any pages, we need to make sure user does have a page
OR "manage_pages" permissions isn't granted yet by the user.
Let's give user an option to grant application permission again.
*/
$login_url = $facebook->get_login_url(array('redirect_uri'=>$home_url,'scope'=>$fb_permissions));
echo '<br />Could not get your page details!';
echo '<br /><a href="'.$login_url.'">Click here to try again!</a>';

}elseif($fbuser && !empty($post_results)){

//Everything looks good, show message form.
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Post to user Page Wall</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>

<div class="fbpagewrapper">
<div id="fbpageform" class="pageform">
<form id="form" name="form" method="post" action="process.php">
<h1>Post to Facebook Page Wall</h1>
<p>Choose a page to post. <?php
/*
Get Log out URL
Due to some bug or whatever, SDK still thinks user is logged in even
after user logs out. To deal with it, user is redirected to another page "logged-out.php" after logout
it is working fine for me with this trick. Hope it works for you too.
*/
$log_out_url = $facebook->get_logout_lrl(array('next'=>$home_url.'logged-out.php'));
echo '<a href="'.$log_out_url.'">Log Out</a>';
?>
</p>
<label>Pages
<span class="small">Select a Page</span>
</label>
<select name="userpages" id="upages">
<?php
foreach ($post_results as $post_result) {
echo '<option value="'.$post_result["page_id"].'">'.$post_result["name"].'</option>';
}
?>
</select>
<label>Message
<span class="small">Write something to post!</span>
</label>
<textarea name="message"></textarea>
<button type="submit" class="button" id="submit_button">Send Message</button>
<div class="spacer"></div>
</form>
</div>
</div>
</body>
</html>
<?php
}
?>

</body>
</html>

That’s all for the index.php file.  Now I’ll create a PHP file for the process to post Facebook page.

Step: 3

I have created a process.php file process to complete posting on my Facebook page. I have used $post_url and $message_body array variable for contains my post URL and message body. This array is stored my post value for posting my Facebook.

Just follows bellows code complete process.php.

<?php
include_once("config.php");

if($_POST)
{
//Post variables we received from user
$user_page_id = $_POST["userpages"];
$user_message = $_POST["message"];

if(strlen($user_message)<1)
{
//message is empty
$user_message = 'No message was entered!';
}

//HTTP POST request to PAGE_ID/feed with the publish_stream
$post_url = '/'.$user_page_id.'/feed';

//posts message on page statues
$msg_body = array(
'message' => $user_message,
);

if ($fbuser) {
try {
$post_result = $facebook->api($post_url, 'post', $msg_body );
} catch (facebook_api_exception $e) {
echo $e->getMessage();
}
}else{
$login_url = $facebook->get_login_url(array('redirect_uri'=>$home_url,'scope'=>$fb_permissions));
header('Location: ' . $login_url);
}

//Show sucess message
if($post_result)
{
echo '<html><head><title>Your Message Posted</title><link href="style.css" rel="stylesheet" type="text/css" /></head><body>';
echo '<div id="fbpageform" class="pageform" align="center">';
echo '<h1>Your message has been posted on your facebook wall.</h1>';
echo '<a class="button" href="'.$home_url.'">Back to Main Page</a> <a target="_blank" class="button" href="https://www.facebook.com/'.$user_page_id.'">Visit Your Page</a>';
echo '</div>';
echo '</body></html>';
}
}

?>

I have created a complete system to post Facebook page using Facebook graph API. If you face a problem to post onto a Facebook page with PHP using graph API, feel free to comment on your suggestions regarding this tutorial without any hesitation.

Authored By Abhigyan Singh

He is a continuous blogger and has blogged on different topic. He loves to surf Internet and always trying to get new Idea about new Technology and Innovations and sharing these great information to all the technology lovers.

Also on DiscussDesk