如何通过facebook graph api获取组的所有帖子

Gen*_*ble 18 facebook facebook-graph-api

我想从facebook组中获取所有帖子并参考其ID.我试过这个:

https://graph.facebook.com/".$group_id."/feed?time_format=U&".$authToken
Run Code Online (Sandbox Code Playgroud)

这只给了我一些帖子.如果可能的话,我希望所有帖子都由一个网址检索,如果不是通过分页网址.但是分页URL包含分页标记

https://graph.facebook.com/55259308085/feed?limit=500&access_token=ACCESS_TOKEN&until=1298025645&__paging_token=55259308085_10150259582898086
Run Code Online (Sandbox Code Playgroud)

什么是分页令牌和直到??

请指引我走正确的道路.谢谢.

Gen*_*ble 16

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

哪个$arr是所有可用分页链接的数组,我使用foreach循环遍历分页的所有内容.
如果需要,我可以提供更多解释.

  • 有机会在Python中发布上述代码吗?那太好了! (2认同)

Nil*_*esh 10

在PHP中试试这个:

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

它会给你一个限制为1000个帖子的页面.将JSON响应转换为PHP关联数组:

$array = json_decode($response, true);
// Do your own stuff with $array ...
Run Code Online (Sandbox Code Playgroud)

获取下一页:

$response = file_get_contents($array['paging']['next']);
Run Code Online (Sandbox Code Playgroud)

在循环中尝试这个,直到$array['data']在响应中显示为空.


直到 - 这是一个UNIX时间整数,用于指定要获取帖子的时间点(仅显示之后发布的帖子).