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);
Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
Eric
  • 5,104
  • 10
  • 41
  • 70

11 Answers11

58

I think this is what you want:

$friends = $facebook->api('/me/friends');
Bad Wolf
  • 8,206
  • 4
  • 34
  • 44
nfvs
  • 1,231
  • 1
  • 11
  • 7
  • 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 ! – José Joel. Oct 08 '10 at 22:13
  • @nfvs, do you know how i can acccess friends_hometown? – alex Jul 15 '11 at 20:39
  • 13
    This no longer works with Graph API v2 or even legacy Graph API v1 apps. See: https://developers.facebook.com/docs/graph-api/reference/v2.0/user/friends "This will only return any friends who have used (via Facebook Login) the app making the request." – dwp4ge May 01 '14 at 14:03
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>';
    }
?>
Levi Putna
  • 2,863
  • 6
  • 30
  • 47
Yash Kumar
  • 279
  • 3
  • 6
  • 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) https://developers.facebook.com/docs/facebook-login/permissions/v2.2 – Web_Developer Jan 06 '15 at 09:04
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>';
}
Elankeeran
  • 6,134
  • 9
  • 40
  • 57
geoffa
  • 143
  • 6
  • 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 '
  • '; – Stevanicus May 10 '11 at 22:15
  • it working as per your suggestion and same updated in the code. thanks Stevanicus & Geoffa – Elankeeran Nov 03 '11 at 19:35