Listing Facebook friends pagewise with PHP

Sometimes, it’s great to see a Facebook user registering on your site, but what if we can show them list of friends in your site, it will be amazing. This is a one page PHP script, that uses facebook PHP sdk to list facebook friends. Friends will be listed pagewise, and how many friends to show per page can also be set. Just change variables on top of script with your own and enjoy. [cc lang=”php”] $appid, ‘secret’ => $appsecret, )); $fbuser = $facebook->getUser(); if ($fbuser) { try { $access_token = $facebook->getAccessToken(); $me = $facebook->api(‘/me’); } catch (FacebookApiException $e) { die($e->getMessage()); } } // Show login form if fresh login requires if (!$fbuser){ $loginUrl = $facebook->getLoginUrl(array(‘redirect_uri’=>$scriptPath)); die(‘Login‘); } $offsetValue=”; $previousurl=”; $nexturl=”; if(isset($_GET[“offset”]) && is_numeric($_GET[“offset”])) { $offsetValue=’&offset=’.$_GET[“offset”]; } $friends = $facebook->api(‘/me/friends?limit=’.$friendsPerPage.$offsetValue); if(isset($friends[“paging”][“next”])) { $nexturlOffset = returnFbOffset($friends[“paging”][“next”]); $nexturl = ‘Next »‘; } if(isset($friends[“paging”][“previous”])) { $previousurlOffset = returnFbOffset($friends[“paging”][“previous”]); $previousurl = ‘« Previous‘; } ?> Friend List ‘.$previousurl.’ | ‘.$nexturl.’
‘; echo ‘
    ‘; foreach ($friends[“data”] as $friend) { echo ‘
  • ‘; echo ‘‘; echo ‘
    ‘.$friend[“name”].’
    ‘; echo ‘
  • ‘; } echo ‘
‘; echo ‘
‘; echo ‘
‘.$previousurl.’ | ‘.$nexturl.’
‘; function returnFbOffset($url) { $str = parse_url($url); parse_str($str[“query”], $strarray); return $strarray[“offset”]; } ?> [/cc] Css to make list appealing: [cc lang=”css”] .fbFriendLists { list-style-type: none; height: 80px; width: 663px; margin: auto; } .fbFriendLists li { float: left; height: 80px; width: 150px; text-align: center; border: 1px solid #DDD; margin: 10px; padding-top: 20px; font-size: 11px; font-family: arial; color: #666666; } [/cc] Demo
  • 9 Comments

    Add Comment
    • Adam
      Fatal error: Class 'Facebook' not found in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\asd.php on line 11
    • Deepak Panda
      "An active access token must be used to query information about the current user" How to fix that error??
    • Macky
      but in cakephp pls help out Call to a member function api() on a non-object
    • Macky
      In cake php where i have to put this code with usermanagemnet plugins from this site i got refrence from ur site. I want show freinds list in my page. codezones.info/social-login-and-simple-acl-plugin-in-cakephp/ plugings contain name,profile name ,image etc but not show freinds list
    • Bjoern
      it just does not :( $scriptPath = 'http://path-to-this/script/'; //path to script what script to you mean??? can you pls give me a demo, i am new with sdks and apis and i need this so fast :( lg bjoern
      • Pranshu Agrawal Bjoern
        $scriptPath is the absolute link to the file on which you are using this code. Its basically acting as the call back URL.
    • Sebastian
      hello, I was wondering if you could build upon this or if would would be interested in working on a project that has this functionality plus a little more stuff added to it...I will even pay you , or if you want to do a tutoria. This demo was AWSOME!
    • Bob
      It would be great if you included how to list them alphabetically, for most applications, they way they are loaded here would be not very user friendy