Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8508307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:07:35+00:00 2026-06-11T03:07:35+00:00

When using the Facebook Graph API to return more than 500 elements (like a

  • 0

When using the Facebook Graph API to return more than 500 elements (like a friend list) paging is required. What’s a good way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T03:07:37+00:00Added an answer on June 11, 2026 at 3:07 am

    Here is the way that I use paging on my own apps.

    http://developsocialapps.com/facebook-friends-list-and-paging/

    The library has most of the code needed. The main method is getGraphObjectWithPaging. It gets the object with the graph API and then keeps looping as long as there is a next page in the response or the $maxpages has been reached. One peculiarity is that sometimes Facebook returns the next page as the same page you just got, so it checks for this and stops at that point too.

    class FacebookApp {               
    
        public $appId;
        private $appSecret;
        private $nameSpace;
        public $userId;
        public $token;
        public $tokenExpires;
    
        // get your own from http://www.w3.org/P3P/
        public $p3p = 'P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"'; 
    
        /*  construct object 
            appid, secret, and namespace from app settings  */
        public function __construct($id, $secret, $namespace) {
            $this->appId = $id;
            $this->appSecret = $secret;
            $this->nameSpace = $namespace;
        }
    
        /*  return json data from a graph api object using paging   
            $object = object to get
            limit = limit parameter for API object
            maxpages = maximum number of pages to get   */
        function getGraphObjectWithPaging($object,$limit=500,$maxpages=10) {
            $data = array();
            $url = $this->getGraphUrl($object,$limit);
            // loop through API calls until maxpages or no paging->next
            while ($maxpages > 0) {
                $response = $this->makeCurlRequest($url);
                if ($repsonse === false) {
                    // something went wrong
                    break;
                } else {
                    $jsonarray = json_decode($response,true);
                    if (isset($jsonarray['error'])) {
                        // something went wrong
                        break;
                    } else {
                        // add current data to data array
                        $data = array_merge ($data,$jsonarray['data']);
                        if (isset($jsonarray['paging']['next'])) {
                            if ($url == $jsonarray['paging']['next']) {
                                // for some reason facebook sometimes returns a next url which is the same as we just got, so exit here
                                break;
                            } else {
                                // keep looping
                                $url = $jsonarray['paging']['next'];
                                $maxpages--;
                            }
                        } else {
                            // no more pages
                            break;
                        }
                    }
                }
            }
            return array("data"=>$data); // using data so it is the same format as other API repsonses
        }
    
        /*  constructs graphs url   */
        public function getGraphUrl($object,$limit=false) {
            $url = "https://graph.facebook.com/".$object;
            if (strpos($url,"?") === false) $url .= "?";
            else $url .= "&";
            $url .= "access_token=".$this->token;
            if ($limit !== false) $url .= "&limit=".$limit;
            return $url;
        }
    
        /*  uses curl to get a url, use $postarray to make a post, otherwise it will get    */
        public function makeCurlRequest($url,$postarray=false) { 
            $return = false;
            try {
                $ch = curl_init(); 
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_HEADER, false); 
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
                if($postarray !== false){ 
                    curl_setopt ($ch, CURLOPT_POST, true); 
                    curl_setopt ($ch, CURLOPT_POSTFIELDS, $postarray); 
                } 
                $response = curl_exec($ch); 
                $responseInfo = curl_getinfo($ch); 
                curl_close($ch); 
                if ($responseInfo['http_code']==200) { 
                    $return = $response; 
                } 
            } catch (Exception $e) {
                $return = false; 
            }
            return $return;
        } 
    
        /*  sets userid and token from signed request, return true or false if authorized   */
        public function initOauthUserFromSignedRequest() {
            $authorized = false;
            if (isset($_REQUEST['signed_request'])) {
                $data = $this->parseSignedRequest($_REQUEST['signed_request']);
                if ($data !== false) {
                    if (isset($data['user_id']) && isset($data['oauth_token'])) {
                        $this->userId = $data['user_id'];
                        $this->token = $data['oauth_token'];
                        $this->tokenExpires = $data['expires'];
                        $authorized = true;
                    }
                }
            }
            return $authorized;
        }
    
        /*  require user to authorize and have permissions for page
            redirect_uri = url to return after user has authorized like redirect.php
            success_uri = url to redirect to on successful authorization like mypage.php
            scope = comma separted list of permissions  */
        function requireAuthorization($redirect_uri,$success_uri=false,$scope=false) {
            if ($success_uri === false) {
                // if no success_uri use current page, all files for app must be in same directory
                $success_uri = substr($_SERVER['REQUEST_URI'],strrpos($_SERVER['REQUEST_URI'],"/")+1); 
            }
            $this->setCookie ("success_uri",$success_uri,0); // we will use this on the redirect_uri page
            $requireauth = true;
            if ($this->initOauthUserFromSignedRequest()) { // user has authorized
                if (($scope === false) || ($this->hasAllPermissions($scope))) { // now check for perms
                    $requireauth = false;
                }
            }
            if ($requireauth) { // user is either not authorized or doesn't have permissions
                $url = $this->getAuthUrl($this->getCanvasUrl($redirect_uri),$scope);
                echo "<html>\n<body>\n<script>\ntop.location.href='".$url."';\n</script></body></html>";
                exit();
            }
        }
    
        /*  checks to see if has permissions, scope is comma separated list */
        public function hasAllPermissions($scope) {
            $return = false;
            $cookiename = "permissions_".$this->appId."_".$this->userId;
            $requiredpermissions = explode(",",$scope);
            // first check cookie
            if (isset($_COOKIE[$cookiename])) {
                $return = true;
                $permissions = json_decode($_COOKIE[$cookiename],true);
                foreach ($requiredpermissions as $perm) {
                    if ($permissions['data'][0][$perm] != 1) {
                        $return = false;
                        break;
                    }
                }
            }
            // if didn't have all in cookie, then see if it is in graph 
            if ($return == false) {
                $permissions = $this->getGraphObject("me/permissions");
                if ($permissions !== false) {
                    $this->setCookie($cookiename,json_encode($permissions),0);
                    $return = true;
                    foreach ($requiredpermissions as $perm) {
                        if ($permissions['data'][0][$perm] != 1) {
                            $return = false;
                            break;
                        }
                    }   
                }
            }
            return $return;
        }
    
        /*  sets a cookie with p3p headers  */
        public function setCookie($name,$value,$expires) {
            if ($this->p3p != '') {
                header($this->p3p);
                $this->p3p = '';
            }
            setcookie ($name,$value,$expires,"/"); 
        }
    
        /*  returns url for oauth authorization
            redirect_uri = url to return after user has authorized
            scope = comma separted list of permissions  */
        public function getAuthUrl($redirect_uri,$scope=false) {
            $url = "https://www.facebook.com/dialog/oauth/?client_id=".$this->appId."&redirect_uri=".rawurlencode($redirect_uri);
            if ($scope !== false) $url .= "&scope=".rawurlencode($scope);
            return $url;
        }
    
        /* returns url to app canvas page, $page like mypage.php?foo=bar    */
        public function getCanvasUrl($page) {
            if ($_SERVER['HTTPS'] == "on") $protocol = "https";
            else $protocol = "http";
            return $protocol."://apps.facebook.com/".$this->nameSpace."/".$page;
        }
    
        /*  parses signed_request parameter and returns data object, returns false if sigs don't match  */
        public function parseSignedRequest($signed_request) {
            list($encoded_sig, $payload) = explode('.', $signed_request, 2); 
            $data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
            $sig = base64_decode(strtr($encoded_sig, '-_', '+/'));
            $expected_sig = hash_hmac('sha256', $payload, $this->appSecret, true);
            if ($sig == $expected_sig) {
                return $data;
            } else {
                return false;
            }
        }
    
    }
    

    Here is how to use it on a page:

    $facebookapp = new FacebookApp($GLOBALS['facebookAppId'],$GLOBALS['facebookAppSecret'],$GLOBALS['facebookNamespace']);
    
    $facebookapp->requireAuthorization($GLOBALS['facebookRedirectPage']);
    
    $friends = $facebookapp->getGraphObjectWithPaging("me/friends");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm using facebook graph api to get the current user friend list. i have
Possible Duplicate: Facebook online friend I am using the Facebook Graph API and I
I'd like to post a Link to Facebook using the Graph API , but
I'm using Facebook Connect along with the Facebook Graph API to fetch user's email
I'm trying to publish checkin using Facebook Graph API. I've gone through Facebook API
Is it possible to send direct message using the facebook graph api? I’ve read
I'm using Koala for Facebook Graph API calls and am using a block for
I'm developing an HTML app that integrates Facebook Graph API using Javascript SDK. Facebook
I am trying to retrieve information through Facebook graph api, using the below code
I created an APP on Facebook and using graph API explorer, I selected my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.