Retrieving posts and comments from a Facebook page

守給你的承諾、 提交于 2019-12-07 07:02:23

问题


I want to retrieve all of the posts from a given facebook page along with the associated comments.

I wrote this code (app details obfuscated, replace with your own in order to run it).

<?php
require_once('facebook.php');
$facebook = new Facebook(array(
    'appId'  => 'MY_APP_ID',
    'secret' => 'MY_APP_SECRET',
    'cookie' => true,
));

$pages = array(
    "stackoverflow" => 11239244970
);

$result = $facebook->api(array(
    'method' => 'fql.multiquery',
    'queries' => '{
        "posts": "select post_id, source_id, actor_id, target_id, likes, message from stream where source_id = '.$pages["stackoverflow"].'",
        "comments": "select post_id, text, username, fromid from comment where post_id in (select post_id from #posts)"
    }'
));

echo json_encode($result);
?>

posts returns the expected results, but comments returns just one comment.

This example queries the stackoverflow facebook page

The comment returned from the comments query is "Joined!" (from this post). I can't figure out what's special about this comment.

Any thoughs?


回答1:


I tried to find a solution playing with Graph Api

https://graph.facebook.com/me/fql?q=select message, comments from stream where source_id = 11239244970

is working but when returning comments it returns only the last 2. It's exaclty how Facebook itself shows the stream and comments with "View all XX comments" link.

To query the posts and comments together can be used:

https://graph.facebook.com/fql?q={"posts":"select post_id,message, comments from stream where source_id = 11239244970","comments": "select post_id, text, username from comment where post_id in (select post_id from #posts)"}&access_token=...

According to Facebook:

Each query of the stream table is limited to the previous 30 days or 50 posts, whichever is greater, however you can use time-specific fields such as created_time along with FQL operators (such as < or >) to retrieve a much greater range of posts.



来源:https://stackoverflow.com/questions/3404396/retrieving-posts-and-comments-from-a-facebook-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!