Getting list of Facebook friends with latest API
Asked Answered
M

11

25

I'm using the most recent version of the Facebook SDK (which lets to connect to something called the 'graph API' though I'm not sure). I've adapted Facebook's example code to let me connect to Facebook and that works... but I can't get a list of my friends.

$friends = $facebook->api('friends.get');

This produces the error message: "Fatal error: Uncaught OAuthException: (#803) Some of the aliases you requested do not exist: friends.get thrown in /mycode/facebook.php on line 543"

No clue why that is or what that means. Can someone tell me the right syntax (for the latest Facebook API) to get a list of friends? (I tried "$friends = $facebook->api->friends_get();" and get a different error, "Fatal error: Call to a member function friends_get() on a non-object in /mycode/example.php on line 129".)

I can confirm that BEFORE this point in my code, things are fine: I'm connected to Facebook with a valid session and I can get my info and dump it to the screen just... i.e. this code executes perfectly before the failed friends.get call:

$session = $facebook->getSession();
if ($session) {
    $uid = $facebook->getUser();
    $me = $facebook->api('/me');
}
print_r($me);
Mallee answered 30/9, 2010 at 21:38 Comment(0)
C
58

I think this is what you want:

$friends = $facebook->api('/me/friends');
Carminacarminative answered 30/9, 2010 at 23:38 Comment(3)
by any chance yo know how to get someone's profile picture with an api call ? something like... $friends = $facebook->api('/idofosmefriend/picture'); And there is some documentation with all of those calls ?, thanks !Incuse
@nfvs, do you know how i can acccess friends_hometown?Ferraro
This no longer works with Graph API v2 or even legacy Graph API v1 apps. See: developers.facebook.com/docs/graph-api/reference/v2.0/user/… "This will only return any friends who have used (via Facebook Login) the app making the request."Selfdevotion
T
23

This is live version of PHP Code to get your friends from Facebook

<?php
    $user = $facebook->getUser();


    if ($user) {
        $user_profile = $facebook->api('/me');
        $friends = $facebook->api('/me/friends');

        echo '<ul>';
        foreach ($friends["data"] as $value) {
            echo '<li>';
            echo '<div class="pic">';
            echo '<img src="https://graph.facebook.com/' . $value["id"] . '/picture"/>';
            echo '</div>';
            echo '<div class="picName">'.$value["name"].'</div>'; 
            echo '</li>';
        }
        echo '</ul>';
    }
?>
Truncation answered 2/4, 2012 at 23:49 Comment(1)
The documentation say that, it shows the friends who also use your app. I have tried above code and got that result. See their link: (Check read_friendlists section) developers.facebook.com/docs/facebook-login/permissions/v2.2Agronomy
S
12

Getting the friends like @nfvs describes is a good way. It outputs a multi-dimensional array with all friends with attributes id and name (ordered by id). You can see the friends photos like this:

foreach ($friends as $key=>$value) {
    echo count($value) . ' Friends';
    echo '<hr />';
    echo '<ul id="friends">';
    foreach ($value as $fkey=>$fvalue) {
        echo '<li><img src="https://graph.facebook.com/' . $fvalue->id . '/picture" title="' . $fvalue->name . '"/></li>';
    }
    echo '</ul>';
}
Siegfried answered 20/10, 2010 at 14:31 Comment(2)
worked! thanks, just one line threw an error for me....in the second foreach loop... i changed it to the following so it worked for me, echo '<li><img src="graph.facebook.com' . $fvalue->id . '/picture" title="' . $fvalue->name . '"/></li>';Granniah
it working as per your suggestion and same updated in the code. thanks Stevanicus & GeoffaLase
S
7

Just a heads up for anyone stumbling across this question using v2.0 of the Graph API: This will not work anymore. In v2.0, calling /me/friends only returns a list the person's friends who also use the app:

  • A user access token with user_friends permission is required to view the current person's friends.
  • This will only return any friends who have used (via Facebook Login) the app making the request.
  • If a friend of the person declines the user_friends permission, that friend will not show up in the friend list for this person.
Sukkah answered 30/5, 2014 at 8:12 Comment(1)
Is there any way to get a list of all friends with the 2.0 Graph API?Mallee
F
3

If you want to use the REST end point,

$friends = $facebook->api(array('method' => 'friends.get'));

else if you are using the graph api, then use,

$friends = $facebook->api('/me/friends');
Flautist answered 10/8, 2011 at 14:56 Comment(0)
S
3

in the recent version of facebook sdk , facebook has disabled the feature that let you access some one friends list due to security reasons ... check the documentation to learn more ...

Selfdriven answered 22/5, 2015 at 11:16 Comment(0)
D
2

Use FQL instead, its a like SQL but for Facebook's data tables and easily covers data query you'de like to make. You won't have to use all of those /xx/xxx/xx calls, just know the tables and columns you are intereseted in.

$myQuery = "SELECT uid2 FROM friend WHERE uid1=me()";
$facebook->api( "/fql?q=" . urlencode($myQuery) )

Great interactive examples at http://developers.facebook.com/docs/reference/fql/

Dehumanize answered 18/11, 2011 at 1:47 Comment(0)
E
0

Using CodeIgniter OAuth2/0.4.0 sparks,

in Auth.php file,

$user = $provider->get_user_info($token);

$friends = $provider->get_friends_list($token);
print_r($friends);

and in Facebook.php file under Provider, add the following function,

    public function get_friends_list(OAuth2_Token_Access $token)
    {
            $url = 'https://graph.facebook.com/me/friends?'.http_build_query(array(
                    'access_token' => $token->access_token,
            ));
            $friends = json_decode(file_get_contents($url),TRUE);

            return $friends;
    }

prints the facebenter code hereook friends.
Elohist answered 21/5, 2013 at 10:37 Comment(0)
M
0
friends.get

Is function to get a list of friend's

And yes this is best

    $friends = $facebook->api('/me/friends');

    echo '<ul>';
    foreach ($friends["data"] as $value) {
        echo '<li>';
        echo '<div class="pic">';
        echo '<img src="https://graph.facebook.com/' . $value["id"] . '/picture"/>';
        echo '</div>';
        echo '<div class="picName">'.$value["name"].'</div>'; 
        echo '</li>';
    }
    echo '</ul>';
Maley answered 2/1, 2014 at 15:3 Comment(0)
M
0

https://graph.facebook.com/v1.0/me/friends?limit=${limit}&access_token=${accessToken}

this api worked for me try this

Murielmurielle answered 26/6, 2021 at 17:21 Comment(0)
R
-3

header('Content-type: text/html; charset=utf-8');

input in your page.

Regurgitate answered 16/5, 2011 at 19:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.