Get Facebook page posts to WordPress with JS SDK or PHP SDK

Solution:1

Use following code

<?php

require_once 'out-fb/src/Facebook/autoload.php';

$app_id = "{app-id}";
$app_secret = "{app-secret}";
$access_token = "{access_token}";

$fb = new Facebook\Facebook([
  'app_id' => $app_id,
  'app_secret' => $app_secret,
  'default_graph_version' => 'v5.0',
  ]);

$response = $fb->get('/{page-id}/posts?fields=message,full_picture,created_time&limit=5', $access_token);

$node = $response->getGraphEdge();

echo $node->getField('3')[message];

Solution:2

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.

 

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.