How to Read Facebook Feed Posts using PHP SDK

by Vincy. Last modified on July 3rd, 2022.

Facebook feed includes posts, links and more data published by the Facebook users. Facebook API contains edges to make connections between data objects. These edges are used to read the collection of objects from Facebook with respect to the user, page as specified in the request URL.

In this example, we have used the {user_id}/feed edge to get the collection of feed data posted by the user or the posts in which the user is tagged.

The Facebook provides SDKs that are used to access API to read and publish updates. The SDKs have differed with respect to the software platform people used from which they call the Facebook API. Let’s use PHP SDK to read Facebook feed posts from the user’s timeline.

Before reading Facebook feeds data, we need to come across with few steps for getting the required access privilege. The steps are listed below to read Facebook feeds from a PHP web application.

  1. Create a new Facebook app and get the App ID and Secret key.
  2. Configure basic settings, privileges.
  3. Get access token by using the App id and the secret key.
  4. Request Facebook feed posts by sending the access token.

Change-Permission-to-Test-User

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.

Reading Facebook Feed using PHP SDK – Output

This output screenshot shows the list of Facebook feed by reading it from the user’s timeline. This feed list is created to show the feed message with the created date and time information.

Reading-Facebook-Feed-using-PHP-SDK-Output

Download

Vincy
Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Comments to “How to Read Facebook Feed Posts using PHP SDK”

  • Joel Zimmerli says:

    Thanks very much for your article.
    As you mentionned, since 4. Sept. we need to get specific permissions in order to get access to fb services.

    I want to create a fb app doing exactely what you describe here. But fb ask me to provide a video to explain how the user will use the app!

    Could you let me know what kind of video you sent to fb in order to get your fb app validated?

    Thanks in advance for your help.
    Joël

    • Vincy says:

      Hi Joël,

      Yes, you are correct. Now we need to send a video to get the app approved. I got this approved pre this conditions were in place. FB has brought in lot of restrictions and eventually making the life of developers difficult.

      Instead of building the complete application through, may be you should develop only a wireframe mockup. Then using the wireframe you can record a demonstration video. I think that should be sufficient to get your FB APP approved. Good luck!

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top

Share this page