Facebook Graph API get all users and their status

17,327

Solution 1

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).

Solution 2

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.

Share:
17,327

Related videos on Youtube

joe_coolish
Author by

joe_coolish

Computer Science major at Brigham Young University. Program in C# and playing with the mobile.

Updated on June 04, 2022

Comments

  • joe_coolish
    joe_coolish almost 2 years

    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 same results as https://graph.facebook.com/me/friends but with the current status included per object.

    Thanks for any help!

    EDIT:

    Here is some clarification. if https://graph.facebook.com/me/friends?access_token=... gets me this: (AKA a list of all my friends)

    {
      "data": [
        {
          "name": "Foo",
          "id": "1"
        },
        {
          "name": "Bar",
          "id": "2"
        },
        {
          "name": "Foo Bar",
          "id": "3"
        },
        {
          "name": "Bar Foo",
          "id": "4"
        }
      ]
    }
    

    what URL or FQL will get me this: (AKA a list of all my friends and their statuses)

    {
      "data": [
        {
          "name": "Foo",
          "id": "1"
          "status": "This is my current status!"
        },
        {
          "name": "Bar",
          "id": "2"
          "status": "This is my current status!"
        },
        {
          "name": "Foo Bar",
          "id": "3"
          "status": "This is my current status!"
        },
        {
          "name": "Bar Foo",
          "id": "4"
          "status": "This is my current status!"
        }
      ]
    }
    

    I only want to make one call, because unlike me, most people have 100+ friends, and I don't want to have to make 100+ calls just to get statuses.

  • Elad Lachmi
    Elad Lachmi about 13 years
    From Facebook documentation about the status table:developers.facebook.com/docs/reference/fql/status for your reference.
  • joe_coolish
    joe_coolish about 13 years
    Not exactly what I was looking for. I updated my post with some clarification
  • joe_coolish
    joe_coolish about 13 years
    Thank you very much, but this is only the status. Is there a way to get the id and name too? The problem is the multiple tables thing with FQL, and I'm not sure how to get around it.
  • Elad Lachmi
    Elad Lachmi about 13 years
    You can get the id. See the edited post. As for the name, there is no way to join tables in FQL, but you can use multiple queries and build the correct json objects yourself. Thats just the way facebook works.
  • Awais Qarni
    Awais Qarni about 13 years
    ok now check my edited answer. But take the externeded permission as I have mentioned above
  • Elad Lachmi
    Elad Lachmi about 13 years
    Tip of the hat to you sir. Very nice answer :)
  • Gublooo
    Gublooo about 13 years
    @ifaour - I was putting together something very similar to your answer. But consider this scenario. Say I have 100 friends and each friend has posted a status message yesterday. But your query2 will still return at most 50 latest posts right? So it cannot be accomplished in one fql call as joe wants or am I going down the wrong path.
  • ifaour
    ifaour about 13 years
    @Gublooo: That's exactly what I meant by my two points at the end. Results may NOT be as expected! But I'm not sure about that because my 100+ friends result returned only 29 status message! I think if Batch API was working as expected it should solve this problem.
  • joe_coolish
    joe_coolish about 13 years
    Thank you very much! I was going to play with multiqueries, but I noticed the whole 50 limit... Is there any way to "skip" the items already received? I'm not too familiar with FQL, so it probably is something simple
  • ifaour
    ifaour about 13 years
    @joe_coolish: What do you mean by items already received? do you mean multiple messages from a single friend? I'm not sure...and I'm not sure if the table limit applies on the the whole query (most likely) or each "uid".
  • ifaour
    ifaour about 13 years
    Once again, I think the new batch api would have made it much simpler. But for some reason it's not working as expected! If you want I could write my findings there too.
  • Zim
    Zim over 12 years
    Thanks Elad -- this works great. One change is "select uid2 from friends where.." to "select uid2 from friend where.."

Related