简体   繁体   中英

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. 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. I would like to know how I can obtain the first posts. Eg: the first 10 posts.

By default the graph API ( https://graph.facebook.com/{group_id}/feed/ ) returns the posts sorted from LAST TO FIRST.

I have read the page about Pagination ( http://developers.facebook.com/docs/reference/api/pagination/ ). So, I know about offset and limit parameters.

There appears to be no method of sorting the feed in any other order.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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