Prajwol Onta
Prajwol Onta

Reputation: 1478

How to get all posts of a group via facebook graph api

I want to grab all the posts from the facebook group with reference to its ID. I tried this:

https://graph.facebook.com/".$group_id."/feed?time_format=U&".$authToken

which only gives me certain posts. I want all the posts to be retrieved by a single url if possible if not by pagination url. But pagination url contains paging token as

https://graph.facebook.com/55259308085/feed?limit=500&access_token=ACCESS_TOKEN&until=1298025645&__paging_token=55259308085_10150259582898086

What is paging token and Until ??

Please direct me to the right path. Thank you.

Upvotes: 21

Views: 54346

Answers (3)

Prajwol Onta
Prajwol Onta

Reputation: 1478

set_time_limit(99999999);
ini_set('memory_limit', "9999M");

function fetchUrl($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 40);
    
    $data = curl_exec($ch);
    curl_close($ch);
    
    return $data;
}

$url_array = array();

function recurse_it($url, $c) {
    global $url_array;
    $feeds         = fetchUrl($url);
    $feed_data_obj = json_decode($feeds, true);
    if (!empty($feed_data_obj['data'])) {
        $next_url      = $feed_data_obj['paging']['next'];
        $url_array[$c] = $next_url;
        recurse_it($next_url, $c + 1);
    }
    return $url_array;
}

$url = "https://graph.facebook.com/55259308085/groups?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$arr = recurse_it($url, 0);
print_r($arr);

Where $arr is an array of all the available pagination links for which I used a foreach to loop through all the contents of the pagination.

Upvotes: 16

Mohan Muthu
Mohan Muthu

Reputation: 51

$url_array=array();
$url="https://graph.facebook.com/v2.2/".$fbpageid."/feed?access_token=".$access_token."&limit=250&fields=full_picture,picture,id,message,name,caption,description,updated_time,link,icon,from,privacy,type,status_type,application,object_id,story,story_tags,actions";

function recurse_it($url,$c)
{
    $feeds=file_get_contents($url);
    $feed_data_obj=json_decode($feeds,true);
    while(!empty($feed_data_obj['data']))
    {   
        $next_url      = $feed_data_obj['paging']['next'];
        $url_array[$c] = $next_url;
        $feeds=file_get_contents($next_url);
        $feed_data_obj=json_decode($feeds,true);
        $c=$c+1;
    }
    return $url_array;
}
$final_result=recurse_it($url,0);

Upvotes: 3

Nilesh
Nilesh

Reputation: 1222

Try this in PHP:

$response = file_get_contents("https://graph.facebook.com/$group_id/feed?limit=1000&access_token=$access_token");

It'll give you a page with a limit of 1000 posts. Convert the JSON response to a PHP associative array:

$array = json_decode($response, true);
// Do your own stuff with $array ...

Get the next page through:

$response = file_get_contents($array['paging']['next']);

Try this in a loop, until $array['data'] comes out as empty in the response.


Until - This is a UNIX time integer used to specify the point in time till which posts are to be fetched (only posts made after that will appear).

Upvotes: 12

Related Questions