facebook php, how do you use results paging?

All of the previous responses are valid.

Here is the way I do, to get all the "next" result with the Graph API:

Note that I don't get "previous" results.

function FB_GetUserTaggedPhotos($user_id, $fields="source,id") {
    $photos_data = array();
    $offset = 0;
    $limit = 500;

    $data = $GLOBALS["facebook"]->api("/$user_id/photos?limit=$limit&offset=$offset&fields=$fields",'GET');
    $photos_data = array_merge($photos_data, $data["data"]);

    while(in_array("paging", $data) && array_key_exists("next", $data["paging"])) {
        $offset += $limit;
        $data = $GLOBALS["facebook"]->api("/$user_id/photos?limit=$limit&offset=$offset&fields=$fields",'GET');
        $photos_data = array_merge($photos_data, $data["data"]);
    }

    return $photos_data;
}

You can change the value of $limit as you want, to get less/more data per call.


I adapted F2000's way of dealing with paging and got following code:

// Initialize the Facebook PHP SDK object:
$config = array(
    'appId'      => '123456789012345',
    'secret'     => 'be8024db1579deadbeefbcbe587c0bd8',
    'fileUpload' => false );
$fbApi = new Facebook( $config );

// Retrieve list of user's friends:
$offset  = 0;       // Initial offset
$limit   = 10;      // Maximum number of records per chunk
$friends = array(); // Result array for friend records accumulation

$chunk = $fbApi->api(
    "/me/friends", 'GET',
    array(
        'fields' => 'id,name,gender',
        'offset' => $offset,
        'limit'  => $limit ) );

while ( $chunk['data'] )
{
    $friends = array_merge( $friends, $chunk['data'] );
    $offset += $limit;

    $chunk = $fbApi->api(
        "/me/friends", 'GET',
        array(
            'fields' => 'id,name,gender',
            'offset' => $offset,
            'limit'  => $limit ) );
}

// The $friends array contains all user's friend records at this point.

The code seems to work. Optionally, for better reliability, it could try to handle temporary connection problems but I skipped this for code clarity.

I am using Facebook PHP SDK 3.1.1


The values in the paging results that you get are the actual URL's that you need to request in order to get the next group of results. For example :

...
{
      "name": "Adobe Flash", 
      "category": "Software", 
      "id": "14043570633", 
      "created_time": "2008-06-05T17:12:36+0000"
    }
  ], 
  "paging": {
      "next" :https://graph.facebook.com/{USER_ID}/likes?format=json&limit=5000&offset=5000
}

This is the "next" paging result I get when I query my user for the pages I like. If I request this URL it will give me a total of 5000 likes with an offset of 5000 (because I already have sen the first 5000 in the initial request). Hope this clarifies things! good luck!