← Back to blog home

Easiest way to get the latest status from facebook fan pages in PHP

09 Nov

So, quite often companies what to show their latest post from their facebook fan page on their website. This should be really simple right. Well it is. However, it seems like most of the world wants to REALLY over complicate it. I searched for this forever and people didn't seem to understand: I don't want facebook connect. I don't need to log in a user. I shouldn't need to load the facebook sdk. I shouldn't need to create a facebook app. There has to be a better way. I thought to myself it's SUPER easy on twitter there has to be something similar for facebook and of course there is.

UPDATE

Facebook changed their system so you need an oAuth token to access graph data. I've updated this post to work with access tokens AND written 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.

 

The basics

Skip all that normal API crap, don't load the Facebook SDK. Just use Facebook Social Graph

http://graph.facebook.com/

To get the latest updates from a fan page (or user with public posts) simply call the following URL:

http://graph.facebook.com/PAGE_USER_ID/feed

 

But how do I get my page user id?

It's very simple just go to your facebook fan page and click your profile picture in the top left. Once you do you'll be taken to a URL that looks like this: http://www.facebook.com/album.php?profile=1&id=XXXXX

after "id=" where the XXXX is will be your user id.

 

PHP Example

This concept should work well using any language of your choice. Below I included a very quick / easy / dirty little PHP function to just get your latest facebook status.

 

	//quick function to get the latest facebook status
	function loadFB($fbID){
		//enter your access token here
		$myFBToken="ACCESS_TOKEN_GOES_HERE";
		//must be https when using an access token
		$url="https://graph.facebook.com/".$fbID."/feed?access_token=".$myFBToken."&limit=1";
		$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);			
		$page = json_decode(curl_exec($c));
		curl_close($c);
		$post=reset($page->data);
		return $post->message;
	}

 

usage is very simple just call loadFB(USER_ID_HERE) and it will return a string containing the latest public status post of the user id you enter.

 

UPDATE

By popular request I wrote a follow up to this post. In this next post I explain how to retrieve multiple entries, filter them so you only see the ones the page owner posted, and display a properly formatted date next to each entry. Read more here: get latest facebook posts part 2

 
 

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. Carl

    November 19, 2010 at 2:18 am

    I can’t seem to get this to work for some reason!

    All it outputs is this:

    data); return $post->message; } echo ‘loadFB’; ?>

    Am I doing something wrong?

    Hope you can help me and thanks in advance :)

     
  2. MBH

    November 24, 2010 at 11:16 pm

    Thanks a bunch! You saved me a lot of headache!

    I wanted to get a variable amount of posts & all I did was pass a limit variable to the function, then comment out the “reset” part of your function.

    In the for loop later, I call messages like this: $data[$i]->messages

    For those who are getting NULL data, make sure you send the FB ID to the function as a string:
    loadFB(“1111111″), otherwise it’ll be sent as integer & get round up!

     
  3. Bold

    November 29, 2010 at 11:17 am

    That’s awesome, JUST what I’me looking for. I’d like to deploy this on a Joomla site, any idea which file I need to use this snippet in? I’m not very savvy with php functions…

     
  4. Faiq

    November 30, 2010 at 2:54 am

    This is for status, how bout to get all wall post in the page? i can get it with the url without ‘limit=1′ but dont know how to read the code using php. Now just can capture if the post is status or photo uploaded and else but limited to just latest post. How to read the feed to get all wall post using php?

     
  5. duminda

    December 9, 2010 at 4:20 am

    can some one help me pls

    FB: data);
    return $post->message;
    echo $post->message;
    }

    ?>

    this is my code it’s not working

     
  6. Lenny

    December 9, 2010 at 8:41 am

    @duminda… That code you posted doesn’t make any sense. Maybe there are some parts missing?

    The first line doesn’t even look like PHP and putting an echo (or anything) after a return will not do anything. I think you may need to learn PHP which is well beyond the scope of this tip it assumes a certain level of PHP experience.

     
  7. SBC

    December 20, 2010 at 1:54 pm

    I was able to get the most recent post to display on my web page but can anyone shed light on how to display the recent 5 posts? TY

     
  8. Lenny

    December 20, 2010 at 2:14 pm

    @SBC

    on the first line: change ?limit=1 to ?limit=5

    on the last line change it to return $page->data;
    and remove the line above “$post=reset($page->data);”

    Then just do a foreach to loop through the results:
    $fbPosts=loadFB(11111);
    foreach($fbPosts as $myPost){
    echo($myPost->message.’<br />’);
    }

     
  9. Bryan

    December 25, 2010 at 8:06 pm

    Great script! Thanks for taking the time to publish it so us noobs can learn from it. I saw the last poster asked how to post the 5 latest feeds and i was wondering how to get the latest 3 or 5 feeds without including other people’s messages or comments. So basically it would only pull in my personal status updates and not other people’s messages or comments to me. Thanks again!

     
  10. Lenny

    December 27, 2010 at 2:32 pm

    I just created a new post that outlines how to do exactly this. In my example I’m displaying the 10 latest posts, but it’s very easy to change the limit to any value that fits your needs. You can check it out here: http://itslennysfault.com/get-latest-status-from-facebook-fan-pages-in-php-multiple

     
  11. kts

    January 3, 2011 at 3:56 pm

    What about OAuth? I am getting oauth errors

     
  12. Lenny

    January 3, 2011 at 4:09 pm

    oAuth is not utilized in this code. This is just to show PUBLIC facebook statuses in the simplest way possible. There should be no need to use oauth.

     
  13. Fadzly

    June 5, 2011 at 11:09 pm

    Hi Lenny,

    Nice work here. Been using it for a few weeks , until this morning. It seems that you now need to have token authorisation in order to get the Public Statuses of any Facebook Fan Page

     
  14. Lenny

    June 7, 2011 at 12:44 pm

    UPDATE: this code has now been updated to utilize oAuth access tokens. Facebook updated their system to require an access token for all news feeds (including ones that are entirely public).

     
  15. Joe

    August 13, 2011 at 8:53 pm

    This looks pretty sweet and is exactly what I want to do, but I can’t get it to work. I get this error:
    Warning: reset() [function.reset]: Passed variable is not an array or object.
    When I try to go to the url I get this message:
    {
    “error”: {
    “type”: “OAuthException”,
    “message”: “An unknown error has occurred.”
    }
    }
    Any help would be greatly appreciated. Thanks.

     
  16. Lenny

    September 21, 2011 at 12:56 pm

    Unfortunately, facebook changed how their system works and this method no longer works. If you look at the top of the entry under “UPDATE” you will see a link to the new method.

     
  17. Anna

    February 5, 2013 at 9:09 pm

    I’m trying to use this on an html webpage and I keep getting a display when uploaded to the server that says:

    data; $fbPosts=loadFB(11111); foreach($fbPosts as $myPost){ echo($myPost->message.’
    ’); }

    Why is it displaying this last bit of the code? What am I doing wrong?