ITS.Picasso
ITS.Picasso

Reputation: 88

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);

    }

Upvotes: 4

Views: 4428

Answers (3)

Ryan
Ryan

Reputation: 24083

As @Alborq mentioned, I think it makes more sense to use a do while loop like this:

$apiData = [];
$response = $fb->get($endpoint);
$edge = $response->getGraphEdge(); 
do {
    $arrayFromThisEdge = $edge->asArray();
    $apiData = array_merge($apiData, $arrayFromThisEdge);
} while ($edge = $fb->next($edge));
return $apiData;

(Unfortunately, in my particular case, I think there might be a Facebook bug.)

Upvotes: 0

Alborq
Alborq

Reputation: 39

Or easy way :

do {
    foreach ($response->asArray() as $item){
        //do something with it
    }
} while($response = $facebookApp->next($response));

Upvotes: 1

Juakali92
Juakali92

Reputation: 1143

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;

Upvotes: 4

Related Questions