← Back to blog home

Get latest status from facebook fan pages in PHP (part 2: multiple entries)

27 Dec

This is a very simple way to get the latest posts from a facebook fan page. I previously wrote a VERY simple guide that just returns the latest single post (as this is all people want a lot of the time). If that is what you are looking for read this guide instead. By popular demand I created this  additional tutorial that will show you how to display the latest 10 posts and display them with date. Also, this code will filter out things posted on your wall by other people and display ONLY things the owner of the facebook page posted.


Facebook changed their system so you need an oAuth token to access graph data. I've updated the code below to use an access token AND wrote an updated tutorial here using rss feeds to pull posts (to avoid needing an access token): Easy way to load Facebook feed through RSS



To use this code you will need to Generate a facebook access token.

We need to ask facebook for more statuses than we need. On the original (simple) example I just retrieved 1 post and displayed it, but since we're going to filter out the ones that other people posted we need to request more than we need. In this example I'm fetching 20 from the facebook server, but only displaying 10.

Also, it is not always the best idea to rely on a 3rd party server for your content. With this code as is it will send a request to facebooks server every time your page is loaded. It is probably a good idea to run a cron every hour or 10 minutes or whatever to get the data from facebook and store it in a database. Then when your page loads you simple retrieve the latest statuses from your own local database. This is well beyond the scope of this tutorial and might not even be needed depending on the kind of traffic your site will be getting, but is provided as friendly advise for optimizing this code.


Lets get to it

Here is the code in it's entirety. I've commented every line to ensure even the most novice PHP programmer can understand what every line of this code does.


//function to retrieve posts from facebook's server
function loadFB($fbID){
	// this is the URL that gets the data from facebook
	// the ?limit=20 tells facebook how many posts we want
	// since we're filtering out some posts you may need to mess with this a bit
	//be sure to update this access token
	//must be https when using an access token
	//load and setup CURL
	$c = curl_init($url);
	curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
	//don't verify SSL (required for some servers)
	curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($c, CURLOPT_SSL_VERIFYHOST, false);			
	//get data from facebook and decode JSON
	$page = json_decode(curl_exec($c));
	//close the connection
	//return the data as an object
	return $page->data;

//BE SURE to enter your facebook id here
//how may posts to show
//variable used to count how many we've loaded

//call the function and get the posts from facebook

//set timezone (change this to your timezone)

//loop through all the posts we got from facebook
foreach($myPosts as $dPost){
	//only show posts that are posted by the page admin
		//get the post date / time and convert to unix time
		$dTime = strtotime($dPost->created_time);
		//format the date / time into something human readable
		//if you want it formatted differently look up the php date function
		$myTime=date("M d Y h:ia",$dTime);
		//output the date / time
		echo("<b>".$myTime."</b> ");
		//output the message body
		//add a line break to separate comments
		echo("<br /><br />");	
		//increment counter
		//if we've outputted the number set above in fblimit we're done
		if($fbCount >= $fbLimit) break;	


The comments in the code above should explain things pretty well. Just make sure you put in your facebook ID. If you don't know how to get your facebook id look here for an explanation.

Also, your server setup must support CURL and JSON for this code to work.




Posted in Guides


Tags: , , ,

Leave a Reply

Notify me of future comments

(I don't spam or share your e-mail. Unsubscribing is as easy as clicking the "unsubscribe" link in the notifications)


  1. Miche

    January 28, 2011 at 4:58 pm

    Thank you. This was the start that I needed….

  2. laverboy

    February 10, 2011 at 6:28 am

    Brilliant, going to use it on our site – thanks!

  3. John

    February 12, 2011 at 10:53 pm

    NICE work Lenny! This was a big help for me. Now, I just need to finish it up so that the anchors/links in my status messages output working links. Thanks for this contribution to the community!

  4. D'nelle

    March 2, 2011 at 1:01 pm

    Thank you so much! i just used this to add updates to one of my clients’ sites and it works like a dream.

  5. Abe

    March 8, 2011 at 8:28 pm

    This is great! Is there a way to have each of the posts be hyperlinked to the original post on Facebook i.e., if anyone clicks on the post, a new page will open with the original Facebook post? Thanks for sharing!!

  6. Abe

    March 8, 2011 at 8:29 pm

    btw, I’ve looked at the other php solution and this is by far the most elegant :-)

  7. Zephni

    April 13, 2011 at 6:54 am

    This is brilliant thankyou. just to let you know there are a couple of variables that are spelt wrong, its is the:

    $fbid, it should be $fbID.

    Just incase anyone copys the whole script and gives up too easily, this works great, just change the two variable names to have a capital “ID”

  8. Ash Blue

    April 15, 2011 at 8:36 pm

    Thank you! You have rescued my weekend from ruin!

  9. Andrew

    April 23, 2011 at 12:17 am

    This works really well with one exception: I get blank entries (just time stamps) for every time I’ve done something other than post on my wall. For example, if I comment on someone else’s post.

    So I get something like…

    Apr 21 2011 10:42pm Having a great time. Wish you were here.

    Apr 20 2011 07:20pm

    Apr 20 2011 06:30pm Planning to have a great time.

  10. Lenny

    May 7, 2011 at 12:16 pm

    Andrew, if you want those wall posts I’m not sure, but if you just want the blank timestamps not to show up just change the following line


    to this

    if($dPost->from->id==$fbid && strlen($dPost->message) > 2){

  11. Michal Ambroziak

    June 3, 2011 at 5:53 pm

    Facebook made a change: Graph API PROFILE_ID/feed and PROFILE_ID/posts requires access_token :(

    I am trying to adopt yours code to meet current requirements, unfortunately without success so far

  12. Kenneth

    June 5, 2011 at 12:57 am

    I’ve been using this code for the past 2 months to pull posts from our public facebook page without a problem. Today however, I started receiving OAuth Token errors. Any idea as to what happened? Even bypassing the code all together and going directly to the graph URL gives the same error.

  13. Kenneth

    June 5, 2011 at 1:21 am

    Well at least I know what the problem is now: Facebook now requires tokens to access graphs. https://developers.facebook.com/blog/post/509
    Now the question is how to update the code to make it work again?

  14. Lenny

    June 5, 2011 at 2:11 pm

    it seems the days of easy to access facebook feeds is over. If anyone comes up with a solution before me let me know. I know the “proper” way to do it is to create a facebook app, but it seems like such a pain. So, I’m on a quest to find an easier way (again).

  15. Lenny

    June 5, 2011 at 3:08 pm

  16. Marcus

    September 22, 2011 at 3:07 pm

    Disregard previous message, I figured it out. See below for anyone else that might be interested…I apologize in advance for my shotty php…

    //output the comment count
    $commCount = $dPost->comments->count;
    //show comments
    if($commCount > 0 && $commCount comments->data;
    foreach($myComm as $dComment) {
    echo ($dComment->message.”\n”);

  17. Dirk

    May 6, 2012 at 11:38 am

    thanks for this work, man. really helpful. but there is a small fault in the following line:

    [...] if($dPost->from->id==$fbid) [...]

    the last variable must be $fbID and not $fbid


  18. AJ Quick

    October 9, 2012 at 10:21 am

    Use /posts instead of /feed… that will return only posts made by you on your page.

  19. jenni

    May 22, 2013 at 1:38 am

    I have tested with the code, But getting the following error.
    Warning: Invalid argument supplied for foreach() in /home/test/public_html/test/test.php on line 40