Search code examples
phpfacebookfacebook-graph-apifacebook-php-sdkinfinite-loop

Facebook SDK (5.0) - Graph API Request - Pagination (How to get ALL results)


I'm working on a simple Facebook app that needs to get ALL of a user's Page likes in order to compare them to a list of pre-built list of Page id's as a matching identifier.

I can get a list of likes; however, I can only get 100 likes "per page" within the response. Some of these people have thousands of likes, so I need to ensure I can collect all of the data from all of the pages.

A simple $fb->next($response); will get me the next page (so instead of 100 likes, I can get 200).. but, when I throw that into a loop my script gets stuck in an infinite loop as there seems to always be a "next" page even though there are no results present.

Is there any built-in way to easily loop through these results, or am I going to have to check the request-level data structure each iteration for whether or not the items have returned data?

//this results in an infinite loop

$request = $fb->request('GET', '/'.$fid['id'].'/likes?limit=9999');

do {
    $graphNode = $fb->getClient()->sendRequest($request);
    $paging = $graphNode->getGraphEdge();
    $graphEdge = $graphNode->getGraphEdge()->asArray();     
    $likes = array_merge($likes, $graphEdge);
} while ($fb->next($paging)); // ERROR IS HERE!!  SET while($paging = $fb->next($paging)); INSTEAD

UPDATED BELOW VIA RESPONSE COMMENT:

        $request = $fb->request('GET', '/'.$fid['id'].'/likes?limit=9999');
    $graphNode = $fb->getClient()->sendRequest($request);
    $graphEdge = $graphNode->getGraphEdge();

    if ($fb->next($graphEdge)) {  

        $likesArray = $graphEdge->asArray();
        $lc = count($likesArray);
        $totalLikes = array_merge($totalLikes, $likesArray); 

        while ($graphEdge = $fb->next($graphEdge)) { 

            $likesArray = $graphEdge->asArray();
            $lic = count($likesArray);
            $totalLikes = array_merge($totalLikes, $likesArray);

        }

    } else {

        $likesArray = $graphEdge->asArray();
        $totalLikes = array_merge($totalLikes, $likesArray);

    }

Solution

  • This is what i've always used. You could always use the loop is a function by changing the $likes variable into $data parameter and passing through other GraphEdge calls through the function.

    $getPages = $this->fb->get('/me/likes?limit=100');
    $likes = $getPages->getGraphEdge();
    
    $totalLikes = array();
    
            if ($this->fb->next($likes)) {  
                $likesArray = $likes->asArray();
                $totalLikes = array_merge($totalLikes, $likesArray); 
                while ($likes = $this->fb->next($likes)) { 
                    $likesArray = $likes->asArray();
                    $totalLikes = array_merge($totalLikes, $likesArray);
                }
            } else {
                $likesArray = $likes->asArray();
                $totalLikes = array_merge($totalLikes, $likesArray);
            }
    
            return $totalLikes;