Facebook Graph API get all users and their status

前端 未结 4 486
清歌不尽
清歌不尽 2020-12-09 12:34

I am using the Facebook Graph API and I was wondering if there was anyway to get a list of all the users and their current status in one call?

I basically want the s

相关标签:
4条回答
  • 2020-12-09 13:00

    hi you just need to take extended permission of user_status or friend_status for this and any call on me object will grab the user current status...

    json_decode(file_get_contents('https://graph.facebook.com/me/friends?access_token='.$cookie['access_token']));
    
    0 讨论(0)
  • 2020-12-09 13:12

    Try the following fql:

    Select uid, message from status where uid in (select uid2 from friends where uid1 = [active user id])
    

    Just make sure to limit to a specific date, because this will return the last 50 updates / or last 30 days of activity by default.

    0 讨论(0)
  • 2020-12-09 13:14

    While the new Batch API should do what you are asking for, I couldn't manage to make it work properly. It seems that it's still buggy.

    Now to achieve this with fql.multiquery, here's a quick example:

    <?php
    $app_id = "APP_ID";
    $app_secret = "APP_SECRET"; 
    $my_url = "REDIRECT_URL";
    
    $code = $_REQUEST["code"];
    
    if(empty($code)) {
    $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" 
      . $app_id . "&redirect_uri=" . urlencode($my_url);
    
    echo("<script> top.location.href='" . $dialog_url 
      . "'</script>");
    }
    
    $token_url = "https://graph.facebook.com/oauth/access_token?client_id="
    . $app_id . "&redirect_uri=" . urlencode($my_url) 
    . "&client_secret=" . $app_secret 
    . "&code=" . $code;
    
    $access_token = file_get_contents($token_url);
    
    $queries = '{"query1":"SELECT uid,name FROM user WHERE uid IN(SELECT uid2 FROM friend WHERE uid1=me())","query2":"SELECT uid,message FROM status WHERE uid IN (SELECT uid FROM #query1)"}';
    $multiquery = "https://api.facebook.com/method/fql.multiquery?queries=" . urlencode($queries) . "&format=json&" . $access_token;
    $res = json_decode(file_get_contents($multiquery), TRUE);
    
    $final = array();
    foreach( $res[1]['fql_result_set'] as $i=>$message_arr ) {
        foreach( $res[0]['fql_result_set'] as $j=>$friend ) {
            if( $message_arr['uid'] == $friend['uid'] ) {
                $friend['message'] = $message_arr['message'];
                $final[] = $friend;
                break;
            }
        }
    }
    print_r($final);
    ?>
    

    Here we are getting the user id and name in the first query and getting the status messages from the second one.
    Now this would return fewer results in the first query or the second! because:

    • A friend may have not posted a status message for the last 30 days, so it won't appear in the second query (second has fewer result set).
    • A friend may have posted more than one status message in the last 30 days (it should return them all - up to 50 ?!!) I'm not sure, but if this is the case then the second will/should return more results than the first one. So I'm getting only one message (notice the break).
    0 讨论(0)
  • 2020-12-09 13:19

    Thanks to @mgilson for his answer, this can be combined to one FQL statement like this:

    SELECT status_id,uid,message FROM status WHERE uid IN (SELECT uid FROM user WHERE uid IN(SELECT uid2 FROM friend WHERE uid1=me()))

    The magic I found is requesting the friends_status permission which will open up from Public posts to Friends and others which help replicate the feeling of the wall.

    0 讨论(0)
提交回复
热议问题