facebook php,你如何使用结果分页?

max*_*ver 15 php facebook facebook-graph-api

您好我正在使用Facebook PHP SDK(v.3.1.1)

我不明白如何使用结果分页url.

我想得到所有朋友的清单,这是我的代码

$friends = $fb->api('/me/friends');
/*  
$friend == Array
(
    [data] => Array
    (
       ...
    ),
    [paging] => Array
    (
        [next] => https://graph.facebook.com/me/friends?method=GET&access_token=SOMETHING&limit=5000&offset=5000
    )
*/
if (!empty($friends['paging']['next']))
{
    $friends2 = $fb->api($friends['paging']['next']);
    //doesn't work
}
Run Code Online (Sandbox Code Playgroud)

F20*_*000 13

以前的所有回复都是有效的.

以下是我使用Graph API获取所有"下一步"结果的方法:

请注意,我没有获得"之前"的结果.

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;
}
Run Code Online (Sandbox Code Playgroud)

您可以根据需要更改$ limit的值,以获得每次调用更少/更多的数据.


Lix*_*Lix 8

您获得的分页结果中的值是您需要请求的实际URL,以便获取下一组结果.例如 :

...
{
      "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
}
Run Code Online (Sandbox Code Playgroud)

这是我向用户查询我喜欢的页面时获得的"下一个"分页结果.如果我请求这个URL,它将给我总共5000个偏移量为5000的喜欢(因为我已经在初始请求中找到了前5000个).希望这能澄清事情!祝好运!

  • 啊所以我必须得到自己那个愚蠢的网址,我以为我应该以某种方式使用facebook库得到它,谢谢! (6认同)

Dar*_*zak 5

我采用了F2000处理分页的方式,得到了以下代码:

// 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.
Run Code Online (Sandbox Code Playgroud)

代码似乎有效.可选地,为了更好的可靠性,它可以尝试处理临时连接问题,但为了清楚代码,我跳过了这个问题.

我正在使用Facebook PHP SDK 3.1.1