简体   繁体   English

如何使用Facebook Graph API获取Group的第一篇帖子

[英]How to get the very first posts of a Group using Facebook Graph API

I have a group on Facebook where the users post funny pictures. 我在Facebook上有一个小组,用户发布有趣的图片。 I am developing a website that will present the posted pictures in a more organised and interesting way. 我正在开发一个网站,以更有条理和有趣的方式呈现发布的图片。

My approach is to use Facebook OpenGraph API. 我的方法是使用Facebook OpenGraph API。 I would like to know how I can obtain the first posts. 我想知道如何获得第一篇文章。 Eg: the first 10 posts. 例如:前10个帖子。

By default the graph API ( https://graph.facebook.com/{group_id}/feed/ ) returns the posts sorted from LAST TO FIRST. 默认情况下,图形API( https://graph.facebook.com/{group_id}/feed/ { https://graph.facebook.com/{group_id}/feed/ }/ https://graph.facebook.com/{group_id}/feed/ )返回从最后到第一个排序的帖子。

I have read the page about Pagination ( http://developers.facebook.com/docs/reference/api/pagination/ ). 我已阅读有关分页的页面( http://developers.facebook.com/docs/reference/api/pagination/ )。 So, I know about offset and limit parameters. 所以,我知道offsetlimit参数。

There appears to be no method of sorting the feed in any other order. 似乎没有以任何其他顺序对Feed进行排序的方法。

The only approach I can see it to download the whole feed and take what you need... 唯一的方法我可以看到它下载整个饲料并采取你需要的...

// Set your access token here...
$accessToken = 'XXX';

// Set your GroupID here...
$groupId = '123';

// Set the number of feed items required here...
$qtyRequired = 10;

$url = 'https://graph.facebook.com/v2.2/' . $groupId . '/feed/?limit=100&access_token=' . $accessToken;
$feed = array();

while ($url) {
    // Get the data from Facebook.
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_URL => $url
    ));
    $data = json_decode(curl_exec($curl));

    // Store the feed to $feed.
    if (is_array($data->data)) $feed = array_merge($feed, $data->data);

    // Load the next page or quit.
    if (isset($data->paging->next)) $url = $data->paging->next;
    else $url = false;
}

// Feed will contain the oldest feed items.
$feed = array_slice($feed, -$qtyRequired);

You might be able to speed it up by specifying the exact fields you need. 您可以通过指定所需的确切字段来加快速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM