getting latest posts from fb using graph api

getting latest posts from fb using graph api

September 15, 2026 · Illustrated author avatarBy Stefan Bogdanescu
getting latest posts from fb using graph api

Getting Latest Facebook Posts Using Graph API

I recently faced a similar challenge where I needed to fetch and display the latest posts from a Facebook page on my Magento website. The issue was that I kept getting an empty array even after setting up a Facebook app with the required permissions.

Let’s dive into the solution step-by-step, assuming you have already set up your Facebook app and obtained the necessary credentials.

Setting Up Your Environment

Firstly, ensure that you are using the correct Facebook PHP SDK version for Graph API v12.0 or later. You can download it from here.

Include the required files and autoload them:

require_once(dirname(__FILE__) . '/facebook/autoload.php');

Initialize a new Facebook instance with your app ID and secret:

$fb = new \Facebook\Facebook([
    'app_id' => '{your-app-id}',
    'app_secret' => '{your-app-secret}',
    'default_graph_version' => 'v12.0'
]);

Obtaining Access Token

To fetch posts from a Facebook page, you need an access token with the pages_read_engagement permission. Here’s how to get it:

$token = $fb->getAccessToken();

If you’re not already logged in, you can obtain a short-lived user-access token like this:

$helper = $fb->getRedirectLoginHelper();
$permissions = ['pages_read_engagement'];
$loginUrl = $helper->getLoginUrl('{your-redirect-url}', $permissions);

Fetching Page Posts

Now, you can fetch the latest posts from a Facebook page using its ID:

try {
    // Replace {page-id} with the actual Facebook page ID
    $response = $fb->get('/{page-id}/posts?fields=id,message,created_time&limit=10', $token);
    $posts = $response->getGraphEdge()->asArray();
    foreach ($posts as $post) {
        echo "Post ID: {$post['id']}\n";
        echo "Message: {$post['message']}\n";
        echo "Created Time: {$post['created_time']}\n\n";
    }
} catch (\Facebook\Exceptions\FacebookResponseException $e) {
    // When Graph returns an error...
    echo 'Graph returned an error: ' . $e->getMessage();
    // Log this error for debugging
    error_log($e);
} catch (\Facebook\Exceptions\FacebookSDKException $e) {
    // When validation fails or other local issues
    echo 'Facebook SDK returned error: ' . $e->getMessage();
}

Understanding the Issue with Empty Array

If you’re still getting an empty array, consider these possibilities:

  1. Missing Permissions: Ensure that your app has the necessary permissions to read page posts. You can add or update them at this link.

  2. Token Expiration: Check if your access token is still valid and hasn’t expired.

  3. Network Issues: Make sure there are no network connectivity problems preventing the API call from succeeding.

  4. Rate Limiting: Facebook enforces rate limits on API calls, so you might hit them if your requests are too frequent. Consider using pagination or reducing request frequency.

Conclusion

To successfully fetch and display the latest posts from a Facebook page on your Magento site:

  1. Set up your environment with the correct PHP SDK.
  2. Obtain an access token with the required permissions.
  3. Use the Graph API to fetch the posts, specifying the fields you need.
  4. Handle exceptions gracefully for debugging purposes.

Remember that Facebook’s Graph API is subject to continuous updates and changes, so always check their documentation for the latest information at developers.facebook.com.

Happy coding!

Security API Configuration Errors

Our network