Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to display latest recent posts in my facebook page to my website

i have page on Facebook and I want to display latest 5 posts from my feed/wall on a page to my website. How to do this? I found this solution.. it is easy

https://developers.facebook.com/docs/reference/plugins/like-box/

and someone guide me to use facebook api and do it myself what is the best way?

I use php mysql to build this site

like image 879
user1080247 Avatar asked May 09 '12 10:05

user1080247


People also ask

How do I get Facebook to show recent posts on my website?

Choose the post you want to show. Click on the top right-hand corner options menu and choose “embed post” Copy and paste the code into your blog or website.

How do I see most recent posts on Facebook on my computer?

Right now, you're in the News Feed. Scroll down and then scroll up. You can now see the Feed Filter Bar with Home (the default one), Favorites, Recent, and an icon. Tap Recent to see recent posts from the pages and people you follow.

Why is my Facebook page not showing recent posts?

If your Facebook feed doesn't appear to be showing the most recent posts, or if some posts which are shared to your Facebook page are missing, then the most likely explanation is that those posts in your feed may be shared from a user's personal Facebook profile or a Facebook page which has an age or location ...


2 Answers

Here is the PHP code. You need to place this in your template.

<ul>
<?php
//function to retrieve posts from facebook’s server
function loadFB($fbID){
    $url = "http://graph.facebook.com/".$fbID."/feed?limit=3";
    // Update by MC Vooges 11jun 2014: Access token is now required:
    $url.= '&access_token=YOUR_TOKEN|YOUR_ACCESS_SECRET';// *

    //load and setup CURL
     $c = curl_init($url);
     curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    //get data from facebook and decode JSON
     $page = json_decode(curl_exec($c));
    //close the connection
     curl_close($c);
    //return the data as an object
     return $page->data;
}

/* Change These Values */
// Your Facebook ID
 $fbid = "190506416472588";
// How many posts to show?
 $fbLimit = 10;
// Your Timezone
date_default_timezone_set("America/Chicago");


/* Dont Change */
// Variable used to count how many we’ve loaded
 $fbCount = 0;
// Call the function and get the posts from facebook
 $myPosts = loadFB($fbid);


//loop through all the posts we got from facebook
foreach($myPosts as $dPost){
    //only show posts that are posted by the page admin
    if($dPost->from->id==$fbid){
        //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);
        ?>
        <ul>
            <li><?php echo($dPost->message) . $myTime; ?></li>
        </ul>
        <?php
        //increment counter
         $fbCount++;
        //if we’ve outputted the number set above in fblimit we’re done
         if($fbCount >= $fbLimit) break;
    }
}
?>
</ul>

Two things you must do for working out this script.

  1. Make sure your server is cURL enabled

  2. You will have change the Facebook ID in the script by yours.

* You can get the access token this way:

$token = 'https://graph.facebook.com/oauth/access_token?client_id='.APP_ID.'&client_secret='.APP_SECRET.'&grant_type=client_credentials';
$token = file_get_contents($token); // returns 'accesstoken=APP_TOKEN|APP_SECRET'
like image 97
Okky Avatar answered Oct 18 '22 21:10

Okky


  1. Login to facebook
  2. Go to facebok developers section "Apps"
  3. Register new app, you need only to register new app, all additional data is optional
  4. Copy your App ID/API Key and App Secret from that same "Apps" section.
  5. Copy facebook.php and base_facebook.php files from repo to your server
  6. Use polymorphic query to api, to request wall content from facebook account

    require 'facebook.php';
    $facebook = new Facebook(array(
        'appId' => 'YOUR_APP_ID',
        'secret' => 'YOUR_APP_SECRET',
    ));
    
    $fbApiGetPosts = $facebook->api('/YOUR_FACEBOOK_ACCOUNT_ID/feed?limit=5');
    if (isset($fbApiGetPosts["data"]) && !empty($fbApiGetPosts["data"])) {
        // display contents of $fbApiGetPosts["data"] array
    }
    

    Replace YOUR_APP_ID with your app ID, YOUR_APP_SECRET with your app secret and YOUR_FACEBOOK_ACCOUNT_ID with target facebook account, you want to get posts from.

Polymorphic query basically is path/URL. More info inside previously mentioned facebook api reference docs.

If your target facebook account wall is public, you won't need anything else that this, to view them.

like image 20
Deele Avatar answered Oct 18 '22 19:10

Deele