Create a new app and get the App id and the secret key
Login with the Facebook developer account and create a new app. We have already seen how to create a new app when we learned about Facebook open authentication using PHP. Get the API key and the app secret id that is created for your app. These two keys will be used later in your PHP code to get the access token for reading Facebook feeds.
Configure basic settings, permission required to access Facebook
The Facebook app has to be configured with some basic settings and permissions required for the remote access. For reading feeds from the user’s timeline, the user_posts permission is required. For each selected permissions we need to upload more details with video screenshot to review. This is to ensure that your application is using the selected permission.
If your Facebook app is in development mode, you can test these things by adding permissions to the test users. The permissions sets with theFacebook test users need not any App review. Select your app in development and navigate through Roles -> Test Users and click the Edit button -> Change permissions this user granted to app. This screenshot shows how to set access permission for the test users.
Get access token by using the App id and the secret key
Integrate Facebook Login for getting user access token by using the App Id and the App secret key. For the test run with the test user, we can get the user id and the access token from the Facebook settings itself.
Request Facebook feed posts by sending the access token
This PHP example shows the consolidated coding flow for reading the Facebook feed post by passing the access token. The Facebook API get() method needs the required Facebook edge /user_id/feeds and the access token as its parameter. This API function will return the object array with the encoded feed data. By using the getDecodedBody() function, the feed data result is decoded. We can iterate the resultant data array to create a Facebook-like wall interface.
<?php
use Facebook\Facebook;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;
require_once './facebook/graph-sdk/src/Facebook/autoload.php';
require_once './facebook/graph-sdk/src/Facebook/Exceptions/FacebookResponseException.php';
require_once './facebook/graph-sdk/src/Facebook/Exceptions/FacebookSDKException.php';
require_once './facebook/graph-sdk/src/Facebook/Helpers/FacebookRedirectLoginHelper.php';
$appId = "YOUR APP ID";
$appSecret = "APP SECRET KEY";
$fb = new Facebook([
'app_id' => $appId,
'app_secret' => $appSecret,
'default_graph_version' => 'v3.1'
]);
$accessToken = "{access-token}";
$postData = "";
try {
$userPosts = $fb->get("/{user-id}/feed", $accessToken);
$postBody = $userPosts->getDecodedBody();
$postData = $postBody["data"];
} catch (FacebookResponseException $e) {
// display error message
exit();
} catch (FacebookSDKException $e) {
// display error message
exit();
}
require_once "wall-view.php";
?>
and the view page to display all the feed data read from the user timeline will show the feed messages with the created date and time. This code is used to create a sample wall with Facebook data.
<style>
body {
width: 550px;
font-family: Arial;
}
.post-item {
border-bottom: 1px #F0F0F0 solid;
padding: 10px;
}
.post-message {
font-size: 1em;
padding-bottom: 8px;
}
.post-date {
color: #b7b7b7;
font-size: 0.9em;
font-style: italic;
}
</style>
<h1>Reading Facebook Feed using PHP</h1>
<?php
if (! empty($postData)) {
foreach ($postData as $k => $v) {
$postDate = date("d F, Y", strtotime($postData[$k]["created_time"]))
?>
<div class="post-item">
<div class="post-message"><?php if(!empty($postData[$k]["message"])) { echo $postData[$k]["message"]; } ?></div>
<div class="post-date"><?php echo $postDate; ?></div>
</div>
<?php
}
}
?>
Note: The source code download doesn’t have the Facebook PHP SDK. Download Facebook PHP SDK and change the path of the SDK root in the file includes added at the beginning of the PHP Facebook example code.