How can I get Facebook feed 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.

How can I get Facebook feed in PHP?
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 developers.facebook.com
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 “phpcluster.com” 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.

Table of Contents

    • To Get Access Token follow the steps given below
    • Page ID can be get as follows with few simple steps:
  • Alternate Approach:

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.

<?php
$page_id = 'YOUR PAGE ID';
$access_token = 'YOUR PAGE ACESS TOKEN';
//Get the data response in JSON
$json_object = @file_get_contents('https://graph.facebook.com/' . $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.

<?php
include "src/facebook.php";
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
//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");
 ?>

<?php
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>";
   } ?>  


Demo

I just need a code sample that collects public posts from a facebook page to a PHP array.

I've created an APP so I have the App ID/API Key and App Secret.

For example, let's say that I want to get all the stackoverflow public posts from Facebook. I've found out that stackoverflow's facebook page id is '11239244970', but now, how do I get all their public posts?

$appKey = '635000000000874';
$appSecret = '567xxxxxxxxxxxxxxxxxxxxxxxxxx3e6';
$fbPage = '11239244970';
$publicFeed = array();

asked Mar 24, 2013 at 20:11

You execute an HTTP GET to PAGE_ID/feed

HTTP GET https://graph.facebook.com/11239244970/feed?access_token=YOUR_ACCESS_TOKEN

https://developers.facebook.com/docs/reference/api/page/#feed

in PHP it can be as basic as

$data  = file_get_contents("https://graph.facebook.com/573948779291487/feed?access_token=YOUR_ACCESS_TOKEN");
$data = json_decode($data, true);

or you can use the PHP SDK.

$publicFeed = $facebook->api('/573948779291487/feed');

To get only the page's own posts and not the fans who like the page, use the /posts endpoint.

answered Mar 24, 2013 at 20:18

How can I get Facebook feed in PHP?

phwdphwd

20k5 gold badges49 silver badges78 bronze badges

4

How can I connect Facebook with PHP?

PHP - Facebook Login.
Login With Facebook. Need to go https://developers.facebook.com/apps/ and click on add a new group button to make the app ID. Choose Website. ... .
fbconfig. php file overview. ... .
Login page Overview. Login page is used to login into FB. ... .
Index. php. ... .
Logout Facebook. Below code is used to logout facebook..

How can I get Facebook page reviews in PHP?

$request = $fb->request('GET', '/'. $page_id. '/'); // Send the request to Graph try { $response = $fb->getClient()->sendRequest($request); } catch(Facebook\Exceptions\FacebookResponseException $e) { // When Graph returns an error echo 'Graph returned an error: ' .

How add Facebook SDK in PHP?

php //initialize facebook sdk require 'vendor/autoload. php'; session_start(); $fb = new Facebook\Facebook([ 'app_id' => '1214799411888113', 'app_secret' => '286f08b36691768f859a70e788f4ceda', 'default_graph_version' => 'v2.

How do I get a Facebook access token?

Obtain User Access Token.
Go to Graph API Explorer..
In Facebook App, select an app used to obtain the access token..
In User or Page, select User Token..
Under Permissions, check ads_read ..
Click Generate Access Token. The box on top of the button is populated with the access token..
Store that token for later use..