How to get your Facebook Feed with graph API using PHP
Today I am going to show you how to pull your Facebook profile feed or any of your friends public feeds, I have filtered it to get only status, photo, video and link sharing with data type filters. To perform this action you need Facebook extended permission of read_stream. This tutorial can help user who want to make application to use Facebook users shared status videos, images and links and call some action ( send SMS or send email ) on each update.
First of all we need to create Facebook login authentication which we already explain in this post How to Login with Facebook Graph API in PHP now this code explain you how to get feeds in graph api.
config.php
Application configuration file, modify CALL BACK URL, Your App ID and Your App Secret values.
<?php $config['callback_url'] = 'CALL BACK URL/?fbTrue=true'; // /?fbTrue=true allow you to code process section. //Facebook configuration $config['App_ID'] = 'Your App ID'; $config['App_Secret'] = 'Your App Secret'; ?>
index.php
Contains PHP code. Get Facebook code and on that code base get access token of current user.
<?php session_start(); require 'src/config.php'; require 'src/facebook.php'; // Create our Application instance (replace this with your appId and secret). $facebook = new Facebook(array( 'appId' => $config['App_ID'], 'secret' => $config['App_Secret'], 'cookie' => true )); $token_url = "https://graph.facebook.com/oauth/access_token?" . "client_id=".$config['App_ID']."&redirect_uri=" . urlencode($config['callback_url']) . "&client_secret=".$config['App_Secret']."&code=" . $_GET['code']; $response = file_get_contents($token_url); $params = null; parse_str($response, $params); $graph_url = "https://graph.facebook.com/me/feed?access_token=".$params['access_token']; $feed = json_decode(file_get_contents($graph_url)); ?>
This code will get access token and make request to graph api to get news feed.
Returned data will be in json so we have converted json to array and create a grid to view data on website.
<?php foreach($feed->data as $data) { if($data->type == 'status' or $data->type == 'photo' or $data->type == 'video' or $data->type == 'link'){ if($data->status_type == 'mobile_status_update'){ $content .= ' <table class="container"> <tr> <td class="profile"><img src="http://graph.facebook.com/'.$data->from->id.'/picture?type=large" alt="'.$data->from->name.'" width="90" height="90"></td> <td class="text"> <strong>'.$data->from->name.' update status</strong><br /> <p>'.$data->message.'</p> <a href="'.$data->actions[0]->link.'">View on Facebook</a> </td> </tr> </table> <div class="clean"></div> '; } elseif($data->status_type == 'added_photos'){ $content .= ' <table class="container"> <tr> <td class="profile"><img src="http://graph.facebook.com/'.$data->from->id.'/picture?type=large" alt="'.$data->from->name.'" width="90" height="90"></td> <td class="text"> <strong>'.$data->from->name.' added a picture</strong><br /> <p>'.$data->message.'</p> <p><img src="'.$data->picture.'"></p> <a href="'.$data->actions[0]->link.'">View on Facebook</a> </td> </tr> </table> <div class="clean"></div> '; } elseif($data->status_type == 'shared_story'){ if($data->type == "link") { $content .= ' <table class="container"> <tr> <td class="profile"><img src="http://graph.facebook.com/'.$data->from->id.'/picture?type=large" alt="'.$data->from->name.'" width="90" height="90"></td> <td class="text"> <strong>'.$data->from->name.' shared a link</strong><br /> <p>'.$data->message.'</p> <table class="link"> <tr> <td valign="top"><a href="'.$data->link.'"><img src="'.$data->picture.'"></a></td> <td> <p>'.$data->name.'</p> <p>'.$data->description.'</p> </td> </tr> </table> <a href="'.$data->actions[0]->link.'">View on Facebook</a> </td> </tr> </table> <div class="clean"></div> '; } if($data->type == "video") { $content .= ' <table class="container"> <tr> <td class="profile"><img src="http://graph.facebook.com/'.$data->from->id.'/picture?type=large" alt="'.$data->from->name.'" width="90" height="90"></td> <td class="text"> <strong>'.$data->from->name.' shared a video</strong><br /> <p>'.$data->message.'</p> <table class="link"> <tr> <td valign="top"><a href="'.$data->link.'"><img src="'.$data->picture.'"></a></td> <td> <p>'.$data->name.'</p> <p>'.$data->description.'</p> </td> </tr> </table> <a href="'.$data->actions[0]->link.'">View on Facebook</a> </td> </tr> </table> <div class="clean"></div> '; } } } } ?>
In above code we have filtered data and show in grid view with its titles, message and images.
First of all we get all data with type photo, status, video, link and then filtered it by its status type and show them on the website. You can add your status’s comments and likes on the same time. It return you complete activity like your comments, likes, friend added, shared status any where on the Facebook every thing you are doing.
You can also get your friends public updates by doing simple changes like below.
// change "me" to your friend Facebook id $graph_url = "https://graph.facebook.com/me/feed?access_token=".$params['access_token'];
Simply change me to some id it will get all public statuses of that person.
We are extracting only latest 25 statuses of yours but you can navigate them by given pagination links for next and previous.
Free download code and a live demo available.
I hope this tutorial helps you so please don’t forget to give us your feedback.
Tutorial Categories:
Nice post,nice demo
Hello, and Thank you for the amazing tutorial!
Just want to ask….is this version working properly for all? I only have a blank page. 🙁 Thanks
try to debug it and send me exact error
Let me help u with obtaining token w/o user interaction. Only server-side request needed.
Just make a curl request to https://graph.facebook.com/oauth/access_token?client_id=…&client_secret=…&grant_type=client_credentials
The result will be a string as follows access_token=blahblahblah.
Wrap that curl request in a function/method and remove the param name, to get the actual token string
str_replace(‘access_token=’, ”, $curlResponce);
That’s it. U are welcome ^_^
Sir I m the biggner I want to show a friend profile is it
possible
What the fuck?? First of all its asking me to subscribe to feedburner even after subscribing I still am not able to download the script!!
I tried changing the me here: // change “me” to your friend Facebook id
$graph_url = “https://graph.facebook.com/me/feed?access_token=”.$params[‘access_token’]; to other id, but not working!
Fatal error: Call to undefined function file_get_contents_curl() in
/hermes/waloraweb002/b2497/pow.huzoorbuxcom/htdocs/phpgang/demo/read-your-facebook-stream-php/index.php
on line 29
not sure if this tut is what im after – I want to have a website (phone apps to follow) that allows me to have the user log in via FB, then display their news feed on the site…however the news feed will be filtered to show posts that have been liked/shared the most by their friends….possible??
Fatal error: Call to undefined function file_get_contents_curl() in /hermes/waloraweb002/b2497/pow.huzoorbuxcom/htdocs/phpgang/demo/read-your-facebook-stream-php/index.php on line 29
Hello the script works well but how can i avoid the login button and get directly feeds from FB without clicking on the login link/button?
You can save access token and use it direct no need to click on buttons all the time.
When tried to execute this, I’m getting below error:
Parse error: syntax error, unexpected ‘class’ (T_CLASS)
Great, all though i had to add a check on the $data->message and description lines because i always run with display_errors on and error reporting E_ALL. how about the css?
pls provide help me
I’m using your aove code, but i have problem in fetching the feeds, i’m getting empty array.
whether is due to Facebook extended permission of read_stream, so could you tell me how i can set the permission.
Your help would be appericiated.
Plz help me as soon as possible.
I’m in between the code..
this script is still working……………………..?
I can’t get the script to run I get Parse error: syntax error, unexpected ‘class’ (T_CLASS) in //index.php on line 62
And line 62 is:
$content .=”’;
I really need this to work – I’ve tried a number of PHP and jquery programmes and none of them work always a small error but my level of PHP knowledge prevents me from diagnosing the issue. I just want some thing simple that works and with a good simple explanation of what each of the components do. In your programme I can’t see anywhere to put the url or page ID of the fedd I want to get (where is that).
I think line 62 should read $content .=”; there is an extra ‘ in the example
No that’s not it still getting the same error.
i got an error “This authorization code has been used” please help me out
hey is it still working ? download not working, can you update a new link ?