How to Get Facebook Feed Using Graph API in PHP

A Facebook page feed can be display on website using Facebook SDK. In this tutorial we will show you how to fetch all your Facebook page feed using graph API and with SDK. It is possible to get Facebook page public feed and you can display on site on any event. In this article you can see how to display custom Facebook feed with Graph API.

Facebook Feed
Facebook Feed

When we try to get interact with facebook for our site we need to create a facebook application. The app you will create from facebook developer section will interact with website and response using graph API. For access of Graph API we must have Facebook SDK file source to include. To create facebook Application we have to follow some steps which are as follows.
Login to
Click on my apps
Click on “add a new app”
Click on website
Type the name of website like “PHPCluster”
Click on “Create a new Facebook App ID”
Choose a category and click on “create app ID” button
Enter the site URL “” and click on “next”
Now Click on my apps and click on PHPCluster option.
Copy two things
App ID and App Secret by click on show button
Now, download the PHP SDK. You can download from the Github
First of all we will see how we can get facebook feed without its SDK for that we require two things which are Page ID and Access token.

To Get Access Token follow the steps given below

Login to facebook developer
Click on Graph API Explorer on left side bar
Click on right side “Get Token”.
Select page token copy and use it.

Page ID can be get as follows with few simple steps:

Click on star icon on top left corner of facebook.
Click on it and select page.
Click on about tab and choose page info,
On bottom of page you will get page ID.
That’s all.
Now put the page id and access token you have copied into given below code.

$page_id = 'YOUR PAGE ID';
$access_token = 'YOUR PAGE ACESS TOKEN';
//Get the data response in JSON
$json_object = @file_get_contents('' . $page_id .
'/posts?access_token=' . $access_token);
//Interpret data
$fbfeeddata = json_decode($json_object);

foreach ($fbfeeddata->data as $post )
echo '<p><a href="' . $post->link . '">' . $post->story . '</a></p>';
echo '<p><img src="'.$post->picture.'">'.'<br>'.'<a href="' . $post->link . '">' . $post->message . '</a></p>';
echo '<p>' . $post->description . '</p>';

Alternate Approach:

Either you can use above code or below alternate code to get Facebook feed.

include "src/facebook.php";
//convert text url into links.
function make_links_to_text_url($text, $class='', $target='_blank'){
    return preg_replace('!((http\:\/\/|ftp\:\/\/|https\:\/\/)|www\.)([-a-zA-Zа-яА-Я0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?!ism', '<a class="'.$class.'" href="//$3" target="'.$target.'">$1$3</a>', $text);
 define("YOUR_APP_ID", 'Your app id here');
 define("YOUR_APP_SECRET",'App Secret');
 define("PAGE_ID",'Your Page ID');

 $config = array(
  'appId' => YOUR_APP_ID,
  'secret' => YOUR_APP_SECRET,    
 $api = new Facebook($config);
 $posts = $api->api("/".PAGE_ID."/posts?limit=10");

foreach ($posts['data'] as $post){
echo "<p>";
echo make_links_to_text_url($post['message']);
echo make_links_to_text_url($post['story']);
echo make_links_to_text_url($post['description']);
echo "</p>";
   } ?>  


Leave a Reply

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