Adding a Facebook news feed to a website

A while back I wanted to add a custom Facebook feed to a website in order to display posts from a client’s Facebook page.  The website was beautifully designed and I didn’t want to ruin the aesthetic by plonking a Facebook branded iframed news feed into it that looks like this.

So I struggled through the Facebook Developers website and after a few weeks of turmoil I was able to put together a PHP script which interacts with the Facebook API and extracts the feed data to allow you to display it however you want!  Here’s a Demo.

You can download the plugin over at Smash Balloon but for those feeling adventurous I wanted to give a brief overview of how to get and display the JSON data from Facebook in case you wanted to try it yourself.

Here’s the basic sequence of events:

1) Register as a developer with Facebook, create an “App” and obtain an Access Token.

You can follow the intructions here which will walk you through how to do that. You need this in order to access the JSON version of a Facebook page’s feed.

2) Find the Page ID of the Facebook page you want to display. 

If you have a Facebook page with a URL like this: https://www.facebook.com/nike then the Page ID is just ‘nike’. If your page URL is structured like this: https://www.facebook.com/pages/name-of-business/123654123654123 then the Page ID is actually the number at the end, so in this case ‘123654123654123′.

3) Access the JSON feed.

Simply put your Page ID and Access Token into the URL below and you now have access to a JSON array of your data.

https://graph.facebook.com/YOUR_PAGE_ID/posts?access_token=YOUR_ACCESS_TOKEN

It’ll look something like this:

facebook-feed-json

4) Get the data and decode it.

$page_id = 'YOUR_PAGE_ID';
$access_token = 'YOUR_ACCESS_TOKEN';
//Get the JSON
$json_object = @file_get_contents('https://graph.facebook.com/' . $page_id . 
'/posts?access_token=' . $access_token);
//Interpret data
$fbdata = json_decode($json_object);

5) Loop through the array of data and display each post

foreach ($fbdata->data as $post )
{
$posts .= '<p><a href="' . $post->link . '">' . $post->story . '</a></p>';
$posts .= '<p><a href="' . $post->link . '">' . $post->message . '</a></p>';
$posts .= '<p>' . $post->description . '</p>';
$posts .= '<br />';
}

 

Putting it all together we get:

$page_id = 'YOUR_PAGE_ID';
$access_token = 'YOUR_ACCESS_TOKEN';
//Get the JSON
$json_object = @file_get_contents('https://graph.facebook.com/' . $page_id . 
'/posts?access_token=' . $access_token);
//Interpret data
$fbdata = json_decode($json_object);

foreach ($fbdata->data as $post )
{
$posts .= '<p><a href="' . $post->link . '">' . $post->story . '</a></p>';
$posts .= '<p><a href="' . $post->link . '">' . $post->message . '</a></p>';
$posts .= '<p>' . $post->description . '</p>';
$posts .= '<br />';
}

Which looks something like this: http://johndoesdesign.com/dev/fbfeed/

To view the basic PHP code behind the plugin then check out the original tutorial here. Or if you want to add photosvideosevents, be able to filter out certain post types and display likesshares and comments then check out the Custom Facebook Feed plugin.

 

Some notes:

1) The Facebook page must be public, meaning that should so be able to access the page without having to log into Facebook. Here’s some more information on how to check whether your page is public or not.

2) If your web server PHP settings have allow_url_fopen set to Off, then file_get_contents won’t work. If this is the case then you have two options: either set it to on by editing your php.ini file (on apache) and setting allow_url_fopen = On, or use an alternative method to get the JSON – such as cURL - as demonstrated here.

 

If you’re struggling to get your feed to work or just don’t want to spend the time figuring out how to display it the way you want then consider purchasing the plugin I built.  It’s available as both a WordPress plugin and PHP Standalone version, is very customizable and allows you to display full-width photos, videos, events, comments and more.

Check out the demo, and if you have any questions then just let me know in the comments below.

241 much appreciated responses...

  1. Andre Dublin says:

    Thank you so much!, I’ve just started using the facebook graph api and they just up and change the dam thing. I appreciate your explanation to help me find how to retrieve my feed.

    • johndoesdesign says:

      Hey no problem, I feel your pain. Glad I could help!

  2. Thank you, Thank you! I don’t do much development work with Facebook, but our non-profit wanted to reduce the duplicate posts between their website and Facebook group and this worked perfectly. Thanks for the great example, it couldn’t have been simpler to figure out!

    • johndoesdesign says:

      Hey Kate, no problem, happy you found the example useful. Checked out your site and looks like you got it working great!

  3. Becca Xiong says:

    Thanks John. I tried your code above. The weird thing is the link works for me in the browser, but in my app, I just get back no response. Any idea?

  4. Becca Xiong says:

    Never mind. Turns out the ‘|’ in the access code may need to be escaped. So what you had works great otherwise. Thanks again!

    • johndoesdesign says:

      Hey no worries, sorry for the late reply, glad you managed to get it figured out.

  5. Kelli says:

    I am a complete novice, but want to feed by FB page to one of the pages in my site, as a shared “blog” and to save me double editing. I have followed your instructions, but have ZERO php or coding experience. I know I am very close to getting the feed to appear, but am missing something in the code. Currently it is showing the below when I view the web page. I think I have followed the previous instructions and have my access token. I am using the digits at the end of our FB page URL: http://www.facebook.com/#!/pages/Dutton-Cycles/173086296043125

    data as $news) { //Explode News and Page ID’s into 2 values $StatusID = explode(“_”, $news->id); echo ‘
    •’ //Check for empty status (for example on shared link only) if (!empty($news->message)) { echo $news->message;} echo ‘
    ‘ } ?>

    I welcome any help you can offer me.

    K

    • johndoesdesign says:

      Hey Kelli, sorry to hear you’re having problems with this.

      First question: As you’re new to PHP, the obvious question would be; is the page that you’re putting this code into a .php page or a .html page?

      Second question: When you visit ’https://graph.facebook.com/PAGE_ID/feed?access_token=ACCESS_TOKEN’ are you seeing an array of data with your status updates in it?

      John

      • Daniel says:

        i’m exactly where Kelli is at. I want to put the feed in an html page. I have gone through all of the steps and I do see news from my FB Group page coming through. So I just need help with that last step, the PHP code. Is this simple to address or do I need to sign up for a PHP class? :/

        • johndoesdesign says:

          Hey Daniel, if you’re getting the JSON feed of the updates then that’s a good sign! My first question would be the same as the one I posed to Kelli.. is the page you’re putting this into a .html page or a .php page. If it’s a .html page (eg: index.html), then change it’s extension to be .php (eg: index.php).

          • Daniel says:

            well, I tried it both ways–html and php. but all I did was copy exactly what you have above and replaced the PAGE_ID and ACCESS_TOKEN. then I got exactly what Kelli got when I tried to open the page with Chrome. so I didn’t add any code at all.

          • Will says:

            I’m also having trouble. I’ve gotten to step 5 and I’m lost. I do run my site in html and css. I hope that wont cause to many problems. I have a page full of this

            “data”: [
            {
            “id”: “100002378684445_137542446335044″,
            “from”: {
            “name”: “Erie Barz”,
            “id”: “100002378684445″
            },
            “type”: “status”,
            “created_time”: “2011-08-15T06:01:12+0000″,
            “updated_time”: “2011-08-15T06:01:12+0000″,
            “comments”: {
            “count”: 0
            }

            and I’m not sure what to do with it.

          • Maylin says:

            I am having the same issue, what I don’t understand is your last sentence, if its a html change it to index.php

            My website is built with wix.com and I have added a HTML widget, and when I followed the above steps, I am seeing this on my page:

            api(‘/me/home?access_token=’.$session['access_token'].’&limit=20′); feed($array); } ?>

            please help, thank you.

      • Lyn says:

        So, how do you use this code if your website use html instead of php? Is there a way to wrap the php code within the html code?

      • Michael says:

        While I understand you can’t get into an explanation of what PHP is and how it works compared to strict HTML here for all of these comments; I think what many of these people probably need is a way to serve this feed without server side scripting.

        Perhaps a javascript to do the same thing? I know enough PHP to be able to follow what you are doing and how to implement it but know very, very little javascript.

        I also have need of something like this as well. I’ve got one site that your feed works great for; yet another which will not run it because the site is hosted on a Windows server with IIS instead of Apache and of course that means it does not have curl and the php installation is probably old enough that it does not have the json interpreter which means feeds like this won’t work.

    • Spencer says:

      Hey Kelli,

      If your problem hasn’t been solved yet, I think whats going on is your not running the page with the PHP through the server. In PHP a server has to be used to interpret the code and if it is not used, like for example when you create a .php/.html page on your desktop and code/open from their all you will see is your code pasted onto the browser. You need to either download a php server that you from from your computer, or test all the .php pages on a live website where the host provides a php server. Also just to clear things up, a .php file is the same as a .html file in structure, you can write html in it but if your using php in the file you need the extension .php so the server knows to read for the “” lines a.k.a. php. Hope this helped.

  6. rubhadubh says:

    note also, that the feed address is ‘https’ when passing a token. Thanks, I was having trouble finding out how to get the access token.

  7. themeforest says:

    We just couldnt leave your website before saying that we really enjoyed the useful information you offer to your visitors

    • johndoesdesign says:

      Cheers, I appreciate that.

  8. Wildsky says:

    This worked for me, but it’s only returning 1 old post. The Next and Previous links that it returns go nowhere.

    This should return ALL items in my feed, right?

  9. bird says:

    Hi,

    thank you for this description.
    This works fine with public profiles or company profiles but when I try to access data from user profiles I only receive an empty data array {“data”: []}. This even happens, when all privacy options are set to “public”.
    When I change the access_token to the one provided on the developers page (using my user account as credential) I receive the full array.

    Do you have any idea if there is a problem with the “grant_type=client_credentials” or the “access_token” at all?

    I found a discussion about this in the fb developer forum: http://forum.developers.facebook.net/viewtopic.php?pid=352799

    • johndoesdesign says:

      Hey Bird, sorry but I’m not sure whether I can help. I did test this with my own profile and I was able to display the data. Are you trying to display data from an account you have access to or from someone else’s?

      • bird says:

        Thanks for your reply!

        I tested this with my own wall as well as with public data feeds from profiles I know.
        In general there is no problem with public profiles of companies or groups but I can’t display private feeds (mine included) at all.

        When eveything is set to “public” I can display the guest comments on my wall but I can’t display my own comments. There seems to be something wrong with the access_token or maybe with the app I’ve created.

        But as pointed out in my comment above, there are a few people experiencing exactly the same problemm with the new kind of authentication.

  10. Kim C says:

    Doesn’t that work only as long as you are logged in? I need to put a feed an a client’s website that pulls from a particular TV Network’s FB page. I can get the json feed as long as I’m logged in – which clearly isn’t going to be a solution for the company I’m doing dev work for.

    Any suggestions? I’ve never worked with Facebook API before and this is making me a little crazy.

    • johndoesdesign says:

      Should work whether you’re signed in or not. I don’t believe you can just grab and display data from anyone’s feed though without their permission. I believe you’d have to request their permission to use their data: http://developers.facebook.com/docs/guides/web/#login

  11. Mohit says:

    Hi,

    I wanted to know if the access token generated from the above method using the appId and appSecret is infinite or weather is expires. I mean will I have to generate that token thru code every time or can I just hard code the once generated token in the URL to get the feed?

    • johndoesdesign says:

      Hey Mohit, it shouldn’t expire so you can just hardcode it into the URL.

  12. Janet says:

    this is tremendously helpful and was easy to follow, and what you’ve made is actually exactly what I need. but, the php code up there isn’t working for me.. I’m trying to put it on the footer.php of my wordpress template. I’m not sure what’s wrong but dreamweaver’s giving me a red flag on the two lines including:

    ( $StatusID = explode(“_”, $news->id); echo ” if (!empty($news->message)) { echo $news->message;} echo ” }

    I’m not sure if what you posted up there was simply a guide or if that’s what you’re using that’s working for you as-is, but from the other comments it sounds like it should work. the browser error says “Parse error: syntax error, unexpected T_IF, expecting ‘,’ or ‘;’” which makes me think maybe it’s a syntax issue?

    Thanks in advance for any advice, and thanks again for posting this how-to.

    • Janet says:

      Figured it out, looks like there needs to be a semicolon after ” and ”

  13. Manu says:

    This is just awesome… I have been trying for 2-3 days to understand how to incorporate facebook feed on my page… and you explained it in such an easy manner… Thank you!

    • johndoesdesign says:

      Hey no probs, glad this helped!

  14. Tru says:

    Thanks Janet for the tip! I had the same issue. It drove me crazy. The section of code should look like this:

    {
    //Explode News and Page ID’s into 2 values
    $StatusID = explode(“_”, $news->id);
    echo ”;
    //Check for empty status (for example on shared link only)
    if (!empty($news->message)) { echo $news->message;}
    echo ”;
    }

    How do you limit the number of post/news articles displayed on you feed?

    Your help is greatly appreciated! Thanks

    • johndoesdesign says:

      I updated the post to show how you can limit the posts by just using an if statement within the loop.

  15. car repos says:

    Hey there, I was just wondering…where do I get the RSS feed for your blog – Sorry about the newbie question if I missed an obvious one…

  16. Nicole says:

    my php is running. I’ve run multiple tests. the URL feed is active when I just load in the browser but when I load my file the page is blank. Looking at my browser activity, its saying internal server error real quick.

  17. Brian says:

    Hi, really need your help here, followed your instruction, but I can’t get the https://graph.facebook.com/PAGE_ID/feed?access_token=ACCESS_TOKEN to work. First of all I have a facebook account, I use it to create an app using the developer. I want to get all the feed from my facebook account, so I replace PAGE_ID with my account name ? Do I need to link my app with my account wall first? Sorry… very lost…
    Thx in advance.

  18. Susheela says:

    Hello John!
    I have followed all your steps but still the page is not displaying anything.I’m not able get where i’m going wrong.
    Another question is that when i’m trying to add news feed from my website,its not updating on facebook….could help me rectify these two errors

  19. George NL says:

    Hello John,

    wonderful tut !! :-) thanks a lot ..

    I have two questions though:

    1. where can i find any documentation on retrieving photo’s, videos etc .. the facebook documentation feels like a maze to me ..

    2. why does the json-request not work proper with jQuery’s getJSON ?

    many many thanks

    g.

  20. spyros says:

    Hello im getting this error T_IF, expecting ‘,’ or ‘;’ in public_html/tweet/facebook/index.php on line 21

    anyone got a clue?

  21. Dab says:

    There is actually a simpler way to do this. There is a framework right now that I use.Its called FourgeFeed.It has everything you guys want and its already setup and ready to embed/put on your own website. Check it out they have a tutorial and download. http://fourgefeed.com/

  22. Alex says:

    Hi there – thanks for putting this up, hard to find any good documentation. I had to add ; behind the to get it work.

    Thanks again!!

  23. Edmond Cembura says:

    Thanks for posting this. I was having trouble trying to integrate Facebook into my site and this was really helpful.

  24. Will says:

    Thanks for the info! This really helps. I didn’t know there was a way to integrate the feed without the crappy box Facebook provides as a default.

  25. Ester Rose says:

    Hi John, txs for the great tutorial. I’m using what you’ve written here to incorporate fb news feeds, the text bit is working now after following the instructions you have put here, but we’d really like to have our pix etc from our fb page coming up as well… could you publish something on that as well? :D

    Ester

  26. Francesco says:

    YEAH! NICE, thanks!

  27. Unresehep says:

    Hey I just wanted to drop a line and say I really like your site. I have been searching at it for really some time now and i thought it was about time i dropped a comment and said hello.

  28. Hadi says:

    Great post.
    Thanks

  29. Obaid says:

    Hi,

    I am trying to access the home of my news feed wall. But It is giving me only my posted comments. And I need all comments posted by my friends etc…

    Thanks
    Obaid…

  30. Dan says:

    Great tutorial! I’ve been hunting for something like this.

    One questions. I’m getting a couple of bullets with no statuses next to them. I’m guessing that’s because they are picture or links. Is there a way to skip over those status updates so the blanks don’t show? Thanks!

    • Andrea says:

      I’m having the same issue with the blank bullets. Also, I’ve adjusted my privacy settings but can’t seem to get posts from myself to show, just from other people.

    • Darko says:

      To get rid of the blank bullets you have to change the code a little bit. Currently it is designed to place a bullet and then check if the status is empty. You have to change it to place a bullet after confirming that the status is not empty. The way you do this is delete the two lines that say echo and echo and change the ‘if’ line to: if (!empty($news->message)) { echo ” . $news->message . ”; }

      That way it will only place a bullet if there actually is a status.

    • Darko says:

      To get rid of the blank bullets you have to change the code a little bit. Currently it is designed to place a bullet and then check if the status is empty. You have to change it to place a bullet after confirming that the status is not empty. The way you do this is delete the two lines that say echo {li} and echo {/li} and change the ‘if’ line to: if (!empty($news->message)) { echo ‘{li}’ . $news->message . ‘{/li}’; }

      (MAKE SURE YOU CHANGE THE { and } to . It wouldn’t let me post code here properly.)

      That way it will only place a bullet if there actually is a status.

      • Janaya says:

        Thanks Darko for your help. I am a student learning to do this and was about to give up working out the li issues when I came across your post and after a few attempts finally worked it out. Awesome! Cheers.

      • Brad says:

        I’ve tried this about 5 different ways, and each time I change the code to the way I think is indicated here, I get a server error.

        • Brad says:

          Never mind, I figured out all the places I needed to replace characters. Both brackets AND quotes/apostrophes.

  31. Eva says:

    Thank you sooooo much. With your tutorial and a couple more I found around I managed to work this out, even though I have absolutely no idea of PhP. Took me a while though ^-^ very happy! Thank you!

    • johndoesdesign says:

      You’re welcome Eva! Glad to help.

  32. Mark says:

    Having an error with the above script. I can see the JSON feed BUT when I put into the page I get.
    Warning: file_get_contents(https://graph.facebook.com/DeshiSkin/feed?access_token=158070850992477|Sy86JLW_iIr5c4yTiu5LbcNhaQ0) [function.file-get-contents]: failed to open stream: No error in C:\websites\deshi\index.php on line 101

    Warning: Invalid argument supplied for foreach() in C:\websites\deshi\index.php on line 105

    What am I missing?

    Thx,
    Mark

    • Mark says:

      Found out file_get_contents wasn’t working on my localhost, now to limit it by 5-6 listing…

      • johndoesdesign says:

        Hey Mark, glad you got it working OK. Mayte’s solution below of adding ‘&limit=5′ to your URL sounds like it should solve your problem.

  33. Terry says:

    Thank you very much for this.

  34. Mayte says:

    this is great… just one favour – how would i limit the result to show only the latest post?

    • Mayte says:

      Hey! never mind i figured it out . i just added “&limit=1″ to the end of the URL

  35. Mike says:

    Hi there,

    you little script seems great but I keep having this error when I try to load my page: Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration …

    can you help?

    thanks

    Mike

  36. ficeball says:

    first i really thank you for the tutorial, its a great help for a newbie like me :)
    but i have one problem on my website. How do i limit the feed to 3-5 lines per status? i have one paragraph status update on facebook page xD

    • johndoesdesign says:

      You could just use PHP to limit the length of the string to 100 characters for example:

      if (strlen($str) > 100) $str = substr($str, 0, 100) . '...';

      • Denise says:

        Hello, I’ve been working through this and it works a treat but now I want to just limit the characters so it doesn’t go over 2 lines.

        I’ve used

        if (strlen($str) > 60) $str = substr($str, 0, 57) . ‘…’;

        but it just seems to be returning nothing now. Any clues? thanks x

  37. amy says:

    Hey this was really helpful..although I would like to know how to put links in the news feed.. i am new to php so do not know much about it

  38. Antonio says:

    helped me 2 install fbwall (1.2.7) plguin.thnx

  39. James says:

    This tutorial looks promising but I’m not sure how I implement it into my HTML code, I don’t want to have the php script in-line because I want my facebook access token to remain secret. Can you show me how I would add this script to my website?

    I have been using but nothing shows up. Maybe I’m completely off but I am really stuck…

    • johndoesdesign says:

      Just paste the PHP code into the php file you want the feed to show up in. PHP is processed on the server-side so only HTML will reach the browser, no one will be able to see your Facebook access token as it isn’t rendered in the HTML.

  40. Rakz says:

    Hi

    I have an error when I embed the php to my page & running in localhost.
    My status won’t display and got this msg:
    Warning: file_get_contents() [function.file-get-contents]: Unable to find the wrapper “https” – did you forget to enable it when you configured PHP? in C:\xampp\htdocs\static\index.php on line 559

    Warning: file_get_contents(https://graph.facebook.com/PAGE_ID/feed?access_token=ACCESS_TOKEN) [function.file-get-contents]: failed to open stream: No error in C:\xampp\htdocs\static\index.php on line 559

    Notice: Trying to get property of non-object in C:\xampp\htdocs\static\index.php on line 563

    Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\static\index.php on line 563

    * I’m not really good in PHP and have no idea what does the error mean.
    Following with the tutorial above, i have list of recent status displayed
    in the page after having the 4.) procedure.

    Can anyone help me? :(
    At number 5.) procedure . I have copy the Php code and replaced
    the line access token of what should be replaced.
    But after posting on it. My page have display of some errors.

    • Khalid says:

      You need to replace with your actual pageid! When you open your page in the browser, write down the long number and it’s your PAGE_ID
      Moreover, file_get_contents expects its parameters “the thing between the paraenthesis” to be either a VARIABLE or a text between quoutes. ie. “rakz”

  41. Clifton Roesch says:

    Thanks so much for sharing this. Quick enquiry, if you don’t mind. Where did you grab your website theme? I’m creating a blog and rather like your websites.

    • johndoesdesign says:

      Cheers Clifton! To answer your question.. I don’t use pre-built themes, I design and build all of my sites – including my own – from scratch. So I guess you could say I grabbed it from my brain.

  42. Darryl says:

    Hi,

    I am building a website and I dont know any code etc. I am using serif x6 as its a drag and drop way to build a site which is very useful for me.

    Do you have a html version I can paste into a html box?

    Thanks

    • johndoesdesign says:

      Hey Darryl, the code is written in PHP, it would need to be placed inside of a .php file in order to be rendered as HTML.

  43. hi. i’m building a website for a nonprofit charity organization. and we are using one.com and their website builder, which does not support PHP any way of doing this without php eg. in HTML?

  44. Amber says:

    Hi
    I get to step 4 and then get nothing.

    {
    “data”: [

    ]
    }
    Not sure what I’m doing wrong? Any thoughts

    • SatissDesign says:

      Same thing for me.
      Do you have a FB page with Age Restrictions: People 18 and over?

      Did you managed how to fix this?

      • Marc says:

        I removed the Country restriction and the age restriction and it works!
        Thank you JohnDoesDesign for this great script

        http://www.kingdommontreal.com
        Enjoy guys ;)

  45. John says:

    Same as Amber above. Nothing is populating.

  46. Mlorenz says:

    This is how to add the FB newsfeed to an .html page
    http://www.bethannonnonprofitmedia.com/add-facebook-feed-to-a-website/

  47. Mubarak says:

    Hi, I’m getting:

    Warning:[FACEBOOK_URL] failed to open stream. No such file or directory.

    What could be the problem? Thanks for sharing the info.

  48. Wesley says:

    thanks for this tutorial. i was wondering how to set the limit of visible posts.

    Thanks

  49. terry s says:

    Hey John,
    I see on the site: http://strategicaircraft.com, you limit the number of status updates to 5.
    How do I do that?

  50. Vince says:

    How do we add images? I can’t find another tutorial.

  51. Justas says:

    excellent, helped a lot, thanks

  52. Andy says:

    Nice one mate…thanks for this FB article, very useful. Sharing is caring as they say :)

  53. Harshad says:

    hi… thanks for the tutorials…
    but here I am stuck on the step of that page or group id…
    I am not getting it..
    please if you would explain it..

  54. SatissDesign says:

    Excellent, but doesn’t work with the only page_ID that I need. :(

    I get to step 4 and then get nothing. I did the test with other PAGE_ID that I own and it work well.

    Is there special configurations inside FB that can block those data. Is it possible it’s because that I have an Age Restrictions: People 18 and over?

    Facebook page
    https://www.facebook.com/KINGDOM.Gentlemans.Club

    Page Id
    114357805248420

    I get
    {
    “data”: [

    ]
    }

  55. Marc says:

    By removing the Country restriction and the age restriction this fantastic code tutorial works perfectly!
    Thank you JohnDoesDesign for this great script

    http://www.kingdommontreal.com
    Enjoy guys ;)

  56. chris says:

    Where should i add the php?

  57. Emily says:

    I am just getting text as if the syntax is incorrect. See the page at http://www.emilydiane.net/ebbg/web-files/index.html and look on the right side under the images. What am I doing wrong? Thanks.

  58. sam roberts says:

    OK, I followed the steps and I get the raw data to publish to my website, but it is just the bland text. I am assuming I need to adapt my CSS to be able to make it look more suitable. Also, is there a way to be able to get it to add the like buttons and other such buttons at the end of the individual posts like it does on the full facebook feed? Also, I noticed you said that it can be done but it was outside the scope of this tutorial but you maybe email me or post on here how to get it to show the images as well. My client updates his facebook page with images and text almost daily and I need to get that to display on his page. I have the feed working from a raw data standpoint. I have it uploaded to my test site right now. if you go to http://www.twmtest.com/about-us.php you can see what I have currently.

    THanks

  59. jouda says:

    I have to test it :)

  60. Csomesz says:

    Thank you so much! ;)

  61. Isaac says:

    I’m having trouble getting past the first step. In creating the App on Facebook it asks me to specify a Canvas URL and Secure Canvas URL. Not sure what that means or what I need to put in that spot.
    Thanks,
    Isaac

  62. Blastoman says:

    Being able to limit the number of posts to just the most recent or any controllable number would be a big help here. Others have been asking as well. Anyone know?

    • johndoesdesign says:

      You should just be able to iterate a variable within the foreach loop and then only display the data when that variable is below a certain threshold. Try something like this:

      $i = 1;
      foreach ($FBdata->data as $news ) {
      if($i < 5){
      $StatusID = explode("_", $news->id);
      echo '<li>';
      if (!empty($news->message)) { echo $news->message; }
      echo '</li>';
      }
      $i++;
      }

    • johndoesdesign says:
  63. Rose says:

    Thank you for this tutorial. But I got some errors. Can anyone help me? Thanks.

    Warning: file_get_contents(https://graph.facebook.com/id/feed?access_token=accesstoken): failed to open stream: Invalid argument

    Fatal error: Call to undefined function: json_decode()

  64. Mike says:

    Hey there. How would this work in relation to having the Facebook feed be blog posts?

  65. hey dude,

    what do you do when the access key expires? On facebook documentation says that the access key is valid for 2hours after that no more feed back.

    “Session has expired at unix team 1349193600. The current unix time is 1349193602.”

    Thx

    • johndoesdesign says:

      Hey Natanael, there are two types of access keys. Application access keys – which we are using here – don’t have an expiry date.

  66. Brian says:

    Awesome..Thanks so much for your help. You summed up quickly what I needed to do and couldn’t find after hunting around facebook documentation for quite a while.

  67. Janaya says:

    Thank you soooo much. Very easy to follow and it works! You mention photos and video can be shown also; do you happen to know where I can find this information? I am creating this for my sister (Im just a student) and would like to be able to learn this.

  68. Stevo says:

    This is awesome. Thanks for saving me from a major headache! Searched online for hours…

  69. Hello, I am recreating this site and want to add their facebook posts to the home page of their website (dalportoranch.com). I use dreamweaver and am not a developer – so how do I add that in dreamweaver? thank you. caroline

  70. sarath says:

    Excellent article!!

    Just would like to add a step:-

    if anyone happens to encounter below issue, please enable extension php_openssl.dll in your php.ini.

    Warning: file_get_contents(https://graph.facebook.com/PAGE_ID/feed?access_token=ACCESS_TOKEN) [function.file-get-contents]: failed to open stream: No error in C:\easyphp\www\index.php on line 431.

  71. Ryan says:

    Is there a way to incorporate a link to the post for each ‘ ‘ that is made? Meaning that each feed entry can be clicked to view the full post on facebook.

  72. Albert says:

    Heyo, Just wanted to drop by and say that I loved your post. In response to a comment above, the Facebook Documentation is a maze and this definitely helped to clear up some issues I was having! Thanks again!

    Couple things to add:

    If you just want to get the statuses of a page:

    When you parse the json using the function json_decode(), it may be easier to return an associative array:

    $jsons = json_decode($JSON_TO_DECODE, true);

    Then, to limit the posts to only a select few, i.e. The first 5 posts on a feed, you can use a for loop:

    function get_messages($num) {
    $messages = array();
    for ($i = 0; $i < $num; $i++) {
    $messages[$i] = $jsons[$i]['message'];
    }
    return $messages;
    }

    $statuses = get_messages(5); //returns the first 5 messages on a given page
    var_dump($statuses); //dump array to console/screen/page

    What do you think?

  73. Alex says:

    Hey John,

    first of all thank you for the code! It has been a great starting-point for me (especially since I´m not really into php…).
    I made a few changes and thought that others might be interested as well:

    1. changed the collected data by replacing “feed” with “posts”; now only the posts of the Site-Owner are shown
    2. if a message is empty (has less than 2 characters) (as it is when a link has been shared) the message “Site-Owner shared a Link” appears
    3. each post that is shown links to the detail page of this post on facebook

    Here´s the code:

    data as $news ) {
    $StatusID = explode(“_”, $news->id);
    $newslink = substr(“$news->id”, 13, 30);

    echo ‘‘ ;

    if (strlen($news->message) > 130) $news->message = substr($news->message, 0, 130) . ‘…’;
    if (strlen($news->message) message = ‘Site-owner shared a Link’;

    if (!empty($news->message)) { echo $news->message; }
    echo ‘
    ‘;
    }
    ?>

  74. Sholac says:

    Hello, can you do this for me? I can pay you via PayPal.

    Thanks

  75. nitrox007 says:

    John,

    Thanks very much for sharing this!

    Question: is there a way to get a direct link to each post? For example, I want to have a link called “read more” that would take you to that particular post.

    Is this something I can build from the data in the feed? Or is there a way to pull the actual links for each post?

    I was able to get the links using the graph api before, but that required me to be logged in.

    Thanks again for your help.

  76. G470 says:

    Nice tutorial :) I´ll try this integration on my next project. Cheers!

  77. Blair Miller says:

    I just wanted to offer a very warm “thank you” for this easy-to-understand example/jumping-off-point. I was merely looking to integrate the most recent Facebook status from our page into the homepage of our forthcoming website. Facebook’s documentation—for someone who isn’t a developer—is indecipherable. Between what you provided, and the MODX plugin TwitterX, I’m up and running. So, thanks!

    • johndoesdesign says:

      Hey you’re welcome Blair. I feel your pain, which is why I put this tutorial together! Glad you were able to get it working as you wanted!

  78. 2046 says:

    hello

    I hevae not read the whole thread but I was lucky with this
    GET https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&grant_type=client_credentials
    from:
    https://developers.facebook.com/docs/opengraph/using-app-tokens/

    where they use the YOUR_APP_ID instead of CLIENT_ID

    thanks for the howto, it helps a lot.

  79. vasu says:

    Hey Expert, I tried above code in PHP to get my FB walls info, but no luck, I Hope you can help me out from this issue.

    Notice: Trying to get property of non-object in C:\xampp\htdocs\fb\index.php on line 12

    Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\fb\index.php on line 12

  80. Jules says:

    For those that want the photos too

    if (!empty($news->picture)) { echo “picture . “‘ alt=’photo’ />”; }

  81. Hubisan says:
  82. Bart says:

    Hi John

    Thanks for the guide, however, I’m stuck getting the actual feed.

    When I try to get the feed from my personal facebook account, i get an empty data response. Do i need to set some permissions or something on the app or on my profile to make it accessible? Any ideas why i would get an empty data response?

    I can get it to work in the graph explorer tool when i use an access token that holds the “read_stream” persmission. However, this access_token expires after an hour or something, so this doesn’t help me much.

  83. Anders says:

    Hi, I get this error:

    Warning: file_get_contents() [function.file-get-contents]: Unable to find the wrapper “https” – did you forget to enable it when you configured PHP? in C:\xampp\htdocs\wfr\index.php on line 40

    Warning: file_get_contents(https://graph.facebook.com/280476038706488&type=1/feed?access_token=566241086735119|GhjeDof8YZ4wl7IXzukkHxJOvr0) [function.file-get-contents]: failed to open stream: No error in C:\xampp\htdocs\wfr\index.php on line 40

    Notice: Trying to get property of non-object in C:\xampp\htdocs\wfr\index.php on line 44

    Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\wfr\index.php on line 44

    I don’t know what it could be, it is in a .php site, so no HTML bugs here :/

    • Anders says:

      Ignore my post, it works, thank you so much, it’s a really good article!

  84. Steven says:

    Dear John,

    thank you.
    Great tutorial, detailed explanation. Brilliant. Saved me hours of investigating facebook for developers.

    Question: do you know where I can learn how to integrate the images in the status? Thanks.

    Congratulations. Hope you find the same help when you want something working.
    Namaste

    Steven

  85. Eddieb says:

    Thanks for the great code, my feed is showing up though it’s minimally formatted. I can see the feed is returning all the replies and everything so now to figure out how to disply the post comments and get some formatting sorted.

    • Eddieb says:

      mmm, turns out the access_token expires after about 2 hours.
      I have an open group and I was wanting a feed on my php page that anyone could view, with a link through for people to register for the group on facebook. Now that the offline option has been deprecated I’m not sure if/how I can manage that as the seperate serverside authentication isn’t really feasible.

  86. Marcus says:

    Great tutorial! But I must be missing something stupid, because I’m still not able to produce output. Any help would be greatly appreciated!

    My JSON output is available:
    https://graph.facebook.com/98302461744/feed?access_token=192029797479889%7Ctovmyx88O8sRspArXzT1QyTO8Lw

    My code is referencing it correctly:
    data as $news ) {
    if($i id);
    echo ”;
    if (!empty($news->message)) { echo $news->message; }
    echo ”;
    }
    $i++;

    //Explode News and Page ID’s into 2 values
    $StatusID = explode(“_”, $news->id);
    echo ”;

    //Check for empty status (for example on shared link only)
    if (!empty($news->message)) { echo $news->message; }
    echo ”;
    }
    ?>

    But I’m still literally getting no output. See:
    http://www.cityofdunedinpipeband.com/facebookfeed.php

    My site is running PHP 5 so all functions should be working. What am I missing? Any help would be greatly appreciated.

    • Marcus says:

      Just noticed not all of the code came through:

      //Get the contents of the Facebook page
      $jsonurl = “https://graph.facebook.com/98302461744/feed?access_token=192029797479889|tovmyx88O8sRspArXzT1QyTO8Lw”;
      $FBpage = file_get_contents($jsonurl,0,null,null);

      //Interpret data with JSON
      $FBdata = json_decode($FBpage);

      //Loop through data for each news item
      $i = 1;
      foreach ($FBdata->data as $news ) {
      if($i id);
      echo ”;
      if (!empty($news->message)) { echo $news->message; }
      echo ”;
      }
      $i++;

      //Explode News and Page ID’s into 2 values
      $StatusID = explode(“_”, $news->id);
      echo ”;

      //Check for empty status (for example on shared link only)
      if (!empty($news->message)) { echo $news->message; }
      echo ”;
      }

  87. bulp says:

    Hi, great code.. but can i get only that “selected FB page post with comments” (not FB standard comment box) ? been output to website ?

  88. Kerry says:

    I’m having the same problem that Obaid posted on May 30th. That is I am trying to access my news feed but this is giving me only my posted comments.I’d like to see the entire news feed as it appears on my facebook page.

    Any suggestions?

    • johndoesdesign says:

      Could be to do with your privacy settings. Which page are you trying to display posts from?

  89. Hemratna Bhimani says:

    Hy…

    Its really useful..
    But i have some problem with that

    I get response in the form of

    {“data”:[]}

    So can you help me??

    • johndoesdesign says:

      Hey Hemratna, it looks like you’re trying to pull data from a personal Facebook page, you can only pull data from a publicly viewable Facebook group/page. For example you could pull data from the NBA page (https://www.facebook.com/nba) by putting ‘nba’ into your link with your access token: https://graph.facebook.com/nba/feed?access_token=ACCESS_TOKEN. But you can’t pull from a personal account due to Facebook’s privacy restrictions.

      • hemratna says:

        Thank you johndoesdesign !!!
        Sorry for late..
        Now i have another question?
        When this access token in expire?
        Or you can say when I have to get another token 2 hours or 2 months (60 days)??

        • johndoesdesign says:

          Individual user access tokens have a default expiration of 2 hours, but an application access token (which we are using for this) does not expire unless you refresh the application secret through your app settings. I have been using the same access token for over 18 months now and it has not expired.

  90. Bob the man says:

    Hi there, it dosent seem to be working.

    I get this output:
    data as $news ) { //Explode News and Page ID’s into 2 values $StatusID = explode(“_”, $news->id); echo ‘
    ‘ //Check for empty status (for example on shared link only) if (!empty($news->message)) { echo $news->message;} echo ‘
    ‘ } ?>

    Which is equal to the data after: foreach ($FBdata->
    Could it be somekind of typeerror?

    • johndoesdesign says:

      Hey Bob, are you able to see the JSON data when you go to the link in step 4?

  91. sahneraum says:

    if you want to show vowel mutations, you can add change this:


    //Check for empty status (for example on shared link only)
    if (!empty($news->message)) { echo $news->message; }
    echo '</li>';

    to this:


    //Check for empty status (for example on shared link only)
    if (!empty($news->message)) {
    $message=rawurldecode(utf8_decode($message));
    echo $news->message;
    }
    echo '</li>';

    • johndoesdesign says:

      Hey thanks, I’ll work that in to my next tutorial example I put together.

  92. hemratna says:

    Can any one explain expire time for this token???

    • johndoesdesign says:

      Individual user access tokens have a default expiration of 2 hours, but an application access token (which we are using for this) does not expire unless you refresh the application secret through your app settings. I have been using the same access token for over 18 months now and it has not expired.

  93. Mary Ann says:

    I got it to work once I loaded it to my hosting server, BUT it looks nothing like your example, just a bulleted list of text no images, no hyperlinks, just the first few words of some posts. What am I doing wrong?
    Thanks-
    Mary Ann

    • johndoesdesign says:

      The code in this tutorial is just for a very basic feed, I’ll be putting together a more advanced tutorial when I get a chance which includes the code for the more advanced example in the demo.

  94. Stu Kendall says:

    I believe I’ve successfully retrieved my App ID, App Secret & Access Token, but when I go to step 4 the page just outputs as:

    {
    “data”: [

    ]
    }

    So, when I use your demo code to add to my website source code nothing comes up. Any suggestions?

  95. freddie says:

    im using the code like this

    data as $news ) {
    //Explode News and Page ID’s into 2 values
    $StatusID = explode(“_”, $news->id);
    echo ”;
    //Check for empty status (for example on shared link only)
    if (!empty($news->message)) { echo $news->message; }
    echo ”;
    }
    ?>

    when going to https://graph.facebook.com/238799632921950/feed?access_token=395390837220613|ZI5NiHDAARkYdh_X-rdIN-IQfj0

    you get the feed how ever when using it in the php it doesnt display any thing its just a blank white page

    http://freddieyoho.com/webdesigns/newsfeed2/newsfeed.php

  96. clinton says:

    Im not getting how to put a feed on there…

    Where are you putting this php. please explain it to me like a 3rd grader?

    I pasted the php in dreamweaver and all the php is blue, it should be red which means there is a goof.

  97. Rogério Madureira says:
  98. Rogério Madureira says:

    Ops, it seams you are getting the data but not displaying it…

  99. Zach says:

    Hi John,

    Thanks for the help… Still having some trouble with this – getting an error

    {
    “error”: {
    “message”: “Invalid OAuth access token.”,
    “type”: “OAuthException”,
    “code”: 190
    }
    }

    Any way you’d be able to assist? Not sure if i’m doing it wrong or it doesn’t work anymore but I just can’t seem to get this thing to work.

    • johndoesdesign says:

      Hey Zach, what’s the ID of the page you’re trying to display?

  100. Hedi says:

    Thank you for this post!

    • johndoesdesign says:

      You are very welcome Hedi!

  101. Hey thanks for the tutorial :)
    But I was wondering whether this will work with the “new” facebook. I’m having trouble showing our newsfeed in the website.
    Help would be really appreciated. :D

  102. wolfdog2rags says:

    This post is exactly what I want to incorporate on our website!
    When I load the code on our site, it displays like this:

    data as $news ) { //Explode News and Page ID’s into 2 values $StatusID = explode(“_”, $news->id); echo ‘
    ‘; //Check for empty status (for example on shared link only) if (!empty($news->message)) { echo $news->message; } echo ‘
    ‘; } ?>

    The following link displays the data/information I am after:

    https://graph.facebook.com/my_page_id/feed?access_token=my_access_token

    I just cannot figure out how to get it to display properly on our website … which powered by: Your Team Look (YTL).

    Any ideas?
    Thanks…

    • johndoesdesign says:

      If you’re just seeing the PHP display on your site as code then it means that you don’t have PHP installed or enabled on your server.

  103. Umair says:

    hey this code works fine in local host but when i upload this to my website on live server it gives me unable to open stream error ?????

  104. Morgan says:

    Thanks so much for this, job done!

    • John B says:

      Hey Morgan, happy to hear that!

  105. Helen says:

    Hiya,

    I’m tearing my hair out here!!!! I have done steps 1 and 2 and got the 2 codes (app id and app secret), I omitted the Client-id etc.. and put my ID numbers in I was given and it won’t go any further!!! I will keep trying…! Helen

    • John B says:

      Hey Helen, sorry to hear that you’re having trouble. I’ve put together a new and improved version which is available for download here. You can try out the demo to see whether it works for you prior to downloading.

      Let me know if you need any help!

  106. Greg says:

    Hi ! Do you know if there is a way to integrate facebook social actions (like, comments, share) to your deported feed ? We managed to integrate like and comment but apparently there is no solution for the share button… We could still add a classic share button but it will dissociate the share action from the original post –> impact on the page insight (impressions, reach…). Thanks for your help :)

  107. Helen says:

    Hiya,

    I’m tearing my hair out here!!!! I managed to do everything but I was having issues with the script. So I decided to try something different and I used my twitter feed script (which was really easy to do!!!) I changed where it says twitter to facebook and changed the ID. It came up on my website as I link ‘posts @algarvecakes’ Its better than nothing at all, but can anyone tell me what I need to add to the script so the posts are visible on the page! ( i tried to add other stuff but nothing works!)

    Tweets by @AlgarveCakes
    !function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=”//platform.twitter.com/widgets.js”;fjs.parentNode.insertBefore(js,fjs);}}(document,”script”,”twitter-wjs”);

    Thanks Helen

    • Helen says:

      OOps! I put the wrong script in my last message!

      posts by @AlgarveCakes
      !function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=”//platform.facebook.com/widgets.js”;fjs.parentNode.insertBefore(js,fjs);}}(document,”script”,”facebook-wjs”);

  108. Billy says:

    Thank you very much, I have no PHP knowledge and very little HTML knowledge a lot of what i have done is with dreamweaver, but using your tutorial in 1.5hour i was able to create http://www.dollyminx.co.uk/fb1.php which I am delighted with, would there be any addional code I could use so if a facebook link is clicked it opens in a new window?
    Thanks again
    Billy

    • John B says:

      Hey Billy, just add target=”_blank” to any link that you’d like to open in a new window/tab.

  109. Denise says:

    Hello, could you give me a tiny bit of help re the character length? I can’t make it limit to 60 which I’d like, followed by the …

    I tried your if (strlen($str) > 100) $str = substr($str, 0, 100) . ‘…’; but it isn’t working, sorry to be a pain. And thanks for the page here so far!

  110. grenade says:

    thank you it did the job well.

    • John B says:

      Glad to hear it!

  111. Hey,
    Thanks for your code.
    But I want to get news feed of a user in my site who logged in through his FB account.

  112. Julie says:

    Hi
    I had your code on my site and it was working well
    But then all of a sudden the images stopped working
    And I notice its the same on your demo too
    Has Facebook changed something that has broken the images?
    Any help appreciated
    Julie

    • John B says:

      Hey Julie, I don’t believe so. It looks like it’s all working fine. It may have been a temporary issue.

  113. Aaron says:

    Thanks for the code. All is working well on my end. I have tried to style the li attribute but I can’t change the padding. I can change color, height, border etc but the padding top and bottom can’t be change. Any ideas or suggestions? Thank you for your effort.

  114. Claire says:

    Thanks for this tutorial. I think it’s the only one I’ve found that shows you how to access your facebook feeds. Fab stuff.

    I found another URL you can use for the feed. You still need an access token, but it’s less numbers to worry about:

    https://graph.facebook.com/USERNAME/posts?limit=5&access_token=ACCESS_TOKEN

    Replace ‘USERNAME’ with your facebook username, and replace ‘ACCESS_TOKEN’ with your access token number.

    Although, with PHP or even a Javascript feed, I can’t get mine to show up. Both the original URL and this one work for pulling up my facebook feed, but neither of them show up on a webpage. I know it’s not the code, because I can make other people’s feeds appear in JS (gotta love view source), but my own feed isn’t showing on my webpage, even though I can view the facebook version of it.

    I’m wondering if it’s the app I created or if it’s just taking a while to populate. Still hunting for the answer to that, but for anyone with ‘blank screen’ the cause is looking as if it is facebook related. *face-desk*

  115. Ivan says:

    Hi John,
    Thanks for sharing your knowledge!
    I’m stuck implementing your techniques.

    1. I don’t know php so I simply post your code as html source code after plugging in PAGE ID and ACCESS TOKEN, right?

    2. What’s the page ID for this https://www.facebook.com/DSC.Beauty/posts/10152787512940243?
    Does one need to have login credentials in order for this to work? I think I’m either not getting the right PAGE ID, or the ACCESS TOKEN is a mismatch, or both?

    Thanks.
    Ivan

  116. Christina says:

    Hi
    I have finally got it to work on my site, but when I enter the &limit=5 it only shows post number five from the feed and not all five.

    Anyone have an explanation ?

    -Christina

  117. John says:

    Hello there,

    thanks for this explanation. I’m having a little issue which has been asked in the past without a proper answer to it.

    I’ve added this code (with my ID and Secret and token thing added to the link already) to an HTML page that I’m currently coding; yet, when I open the html file in my browser, it shows this part of the code:

    data as $news) { //Explode News and Page ID’s into 2 values $StatusID = explode(“_”, $news->id); echo ‘
    •’ //Check for empty status (for example on shared link only) if (!empty($news->message)) { echo $news->message;} echo ‘
    ‘ } ?>

    I’m not sure what’s wrong with this because I even tried saving this file as a different file (outside of the html one) and called it with a

    With the exact same result…any suggestion?

    Oh, by the way, if I go to the link with my ID and token, I do see the array with all the info there…

    Thanks for any help

    • It may be because you are running the page that is saved as a .html file. In order for PHP to work it needs to be saved as a .php file.

  118. Pingback: Facebook feed hell | Anita Luckett

  119. Liz Donlan says:

    A client has asked me to put his Facebook newsfeed onto his website. I thought I’d try it on my own website first and was pleased to find your original posting which I have followed.

    I have just scrolled to the end of the 200 comments you’ve received and have just seen your lastest comment stating what you get in your browser – that is exactly what I have just got so I look forward to seeing any responses!

    Liz

  120. Willz says:

    Thanks a lot! Works perfectly!

  121. Andy H says:

    Hello I have done this
    http://www.cheshirerailways.co.uk/experiment

    and i get this as a reply:

    - start –
    Fatal error: Call to undefined function: json_decode() in /homepages/20/d357031760/htdocs/cheshirerailways/experiment/index.php on line 7
    - end -

    Please help!

    • John B says:

      Can you try out the demo here and let me know whether it works for you there?

  122. Joe says:

    this is amazing. thank you so much. The only thing I need is the fact that the text font size is too large. is there any code to add to the PHP or it should be separately done through CSS? any easy way to adjust it?

    • John B says:

      CSS is the way to do it. Just find out which element you need to change the font size for and use font-size: __px or __em to adjust it.

  123. Kicker says:

    Hey JDD! – well – I’m having an odd problem myownself. I can read the feed (or posts); however, certain items on the timeline do not show up in the response returned by https://graph.facebook.com/kicker/feed?access_token=myToken

    if you look at the kicker fb page, you’ll see a plain old text status post which does not show i the feed

    EVEN more odd, the only thing that DOES show in the returned feed data is a post that was deleted from the timeline??

    Shares show up – if I share a web page or a video etc. But simple text posts or picture uploads etc. do not.

    A sample page including my fb read stuff is here… (only intended to show the latest post … currently shows a deleted post from last month … LOL!) http://www.tennispensacola.com/clubs/Roger-Scott-Tennis-CenterFB1.htm

    I see the same effects in your demo … confused? (not a totally unknown condition :(:

    • John B says:

      Hey Kicker,

      Which feed are you trying to display?

      - https://www.facebook.com/kicker
      or
      - https://www.facebook.com/kickersJournal

      The first is a profile, not a page, and so the data isn’t publicly available.
      The second is a public page (which is good) but it only appears to have one post on it’s Facebook page: ‘dummy post while testing stream read…’ and so that’s all that’s showing up in the demo.

      John

      • Kicker says:

        Hey John – thanks – Yeah – tried both … FINALLY found some info that profiles respond differently to stream read requests than do “pages.” … simple text status posts on profiles are not included in stream read requests, for example – whereas, they are included for “page” stream read requests.

        kickersJournal is a dummy page set up to play with page features – and yeah – I saw that the text post was returned. Profiles are much more restrictive in what they include in the stream read request but that’s ok for me – I was doing it for businesses anyway.

        Still – thanks again for the response … now off to figure out how to format an event posting LOL!!

  124. Mike says:

    Hey John!

    Sorry to bother you with this but i’m quite confused. I have access to Json feed.
    I tried to copy and paste your code above and everything i get is:

    Warning: Invalid argument supplied for foreach()

    Any ideas how to fix this?

    • John B says:

      Hey Mike, no bother at all.

      See note #2 at the bottom of the post. It sounds like file_get_contents isn’t able to run as you have allow_url_fopen disabled in your php.ini file. You can either enable it or alternatively use cURL:

      $url = ‘https://graph.facebook.com/’ . $page_id . ‘/posts?access_token=’ . $access_token;

      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_TIMEOUT, 20);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      $json_object = curl_exec($ch);
      curl_close($ch);

      Let me know whether that works for you.

      • Mike says:

        Thank you very much John. cURL did the trick.

        • John B says:

          Awesome glad to hear it!

  125. How can I retrieve the facebook group ID? Everyone talks about Facebook Pages and I saw that there are three locations were it talks about Facebook Groups. But nowere on how to get the ID for Facebook Groups?

    • John B says:

      Hey Gustavo, depending on the group it’s going to be the number in the URL which comes after facebook.com/groups/1234567890.

      The script in this tutorial likely won’t work with groups though as Facebook groups aren’t public, meaning that you need to be signed into Facebook in order to view them.

  126. LJ says:

    Hey John,
    just wanna say thanks a lot! it’s so helpful.

  127. Kyle says:

    Howsit :)
    This would be amazing if i could work out how i broke it ..

    Iv tested my feed in the demo section, and that works perfectly,
    Iv enabled fopen_url, iv tried the cURL technique
    iv made a new app id,
    Iv made a new server with everything open

    And still my page comes up blank, it is a php page, with no other plugins running, just this feed system.

    Iv been tryng to crack this facebook thing for nearly a week now, any feedback would be greatly appretiated

    • John B says:

      Hey Kyle,

      Sure. Are you using the script from the tutorial or the Custom Facebook Feed WordPress plugin?

      John

  128. Shreyo says:

    hello John,
    after stumbling a lot of websites, i was brought down to this great page and thanks for the code. your code works without any obstacles.
    i wanted to know that how can i load more feeds; it loads the last 15-16 feeds from a profile. i tried working with the next and previous links from the json data but i’m lost now.
    pls help.

    thanks for the code again.

    • John B says:

      Hey Shreyo,

      You can add the ‘limit’ parameter to the query string which will retrieve more posts from the API:

      $json_object = @file_get_contents(‘https://graph.facebook.com/’ . $page_id .
      ‘/posts?access_token=’ . $access_token . ‘&limit=50′);

      John

      • Shreyo says:

        thanks for the reply, John. I succeeded fetching 47 feeds (exact) on setting limit to 100. I don’t understand why this happens, i went onto fb documentation, though.

        -shreyo-

  129. adjiri says:

    hey. Im new to php. I just set up the facebook feeds. However i dont see user “NAME”. How do i do that? HELP PLEAESE

  130. Richard says:

    So I reg as FB developer and tried to create App token but FB kept taking me to my FB page?
    Is this because my FB account is is a business?

    I tried to create another with private FB account but will not let me use same mobile number to reg!!

  131. Joel says:

    I can successfully retrieve the feed, but get this error message and then nothing displays:

    Notice: Undefined property: stdClass::$link in /var/www/vhosts/news.domain.com/htdocs/facebook_feed.php on line 23

    Notice: Undefined property: stdClass::$link in /var/www/vhosts/news.domain.com/htdocs/facebook_feed.php on line 24

    Notice: Undefined property: stdClass::$message in /var/www/vhosts/news.domain.com/htdocs/facebook_feed.php on line 24

    Notice: Undefined property: stdClass::$description in /var/www/vhosts/news.domain.com/htdocs/facebook_feed.php on line 25

    etc…….

    My server is running PHP 5.2.16 and json version 1.2.1.

    Any ideas what I can do?

  132. Joel says:

    Never mind – programmer error!

  133. taqi says:

    thank you :)

  134. Birsu Ionut says:

    Hey john,
    Your script is awesome, but i don’t know why it gives me some errors :(
    Notice: Undefined property: stdClass::$story in D:\xampp\htdocs\test\facebook\index.php on line 20

    Notice: Undefined variable: posts in D:\xampp\htdocs\test\facebook\index.php on line 20

    Notice: Undefined property: stdClass::$description in D:\xampp\htdocs\test\facebook\index.php on line 22

    Notice: Undefined property: stdClass::$message in D:\xampp\htdocs\test\facebook\index.php on line 21

    Notice: Undefined property: stdClass::$description in D:\xampp\htdocs\test\facebook\index.php on line 22
    i used cURL, cuz otherwise it gives me error at :
    foreach ($fbdata->data as $post ) line… can you help me?

  135. abdullah says:

    Thats of such a great help John..thanks a ton for your help..
    This thing works fine as far as i m pulling data from a facebook page but it doesnt work when i start pulling data from a users profile..
    Any idea for why is that happening..
    Thanks
    Abdullah K

    • John B says:

      Hey Abudullah, you can’t use an app access token to display posts from a personal profile. In order to do that you’d need a user access token which expire after a certain period of time. You can get a user access token via the Facebook Explorer.

  136. Simon says:

    I’m getting a Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 11. Tried a few things to fix it but they’re not working. Any suggestions?

    • Simon says:

      Ok, sorry, that was actually me not realizing that using the [php] shortcode I still needed to use PHP headers and footers before I started inputting commands; however it’s not displaying any posts on page now but not giving me any errors, nothing coming up on console.

      • Simon says:

        And one final note: if I load the PHP page directly it DOES display facebook data. However the problem I’m facing is getting that PHP page to include on pageloads at the target html page.

        • Simon says:

          Nevermind. I figured it out. All good!

Feel free to add your comment..

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>