Monday , 29 May 2017

How to Get Friends List Using Facebook API

Get Friends List Using Facebook API

Here it is another tutorial on Facebook app development and in this article I am going to share you that how you can get friends list  of any user using Facebook API (Application Programming Interface) so hope you will like it because you can use it in different ways like you can get friends list of any user who will use your app even if the user has made friends list visible only to him it’s like exposing friends on Facebook. You can use this method in many different ways but I am not going to describe all of them because you already know the importance of this article if you are beginner Facebook app developer.

Facebook API: Get Friends List

If you do not know a bit about Facebook app developing then read develop FB app using PHP SDK to learn basics of Facebook application development. So, now let me show you the code then I’ll explain it to you. Read another article to integrate Facebook with WordPress blog.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<!DOCTYPE html>
<html>
    <head>
        <title>Your App Title</title>
    </head>
<body>
 
<?php
    //facebook application configuration
    $fbconfig['appid' ] = "APP ID HERE";
    $fbconfig['secret'] = "APP SECRET HERE";
 
    $fbconfig['baseUrl']    =   "http://roomi.orgfree.com/etb/"; //put your canvas URL here;
    $fbconfig['appBaseUrl'] =   "http://apps.facebook.com/et_blog/"; //put your URL provided by Facebook;
 
    /* 
     * If user first time authenticated the application facebook
     * redirects user to baseUrl, so I checked if any code passed
     * then redirect him to the application url 
     */
    if (isset($_GET['code'])){
        header("Location: " . $fbconfig['appBaseUrl']);
        exit;
    }
    //~~
 
    //
    if (isset($_GET['request_ids'])){
        //user comes from invitation
        //track them if you need
    }
 
    $user            =   null; //facebook user uid
    try{
        include_once "src/facebook.php";
    }
    catch(Exception $o){
 
        print_r($o);
 
    }
    // Create our Application instance.
    $facebook = new Facebook(array(
      'appId'  => $fbconfig['appid'],
      'secret' => $fbconfig['secret'],
      'cookie' => true,
    ));
 
    //Facebook Authentication part
    $user       = $facebook->getUser();
    // We may or may not have this data based 
    // on whether the user is logged in.
    // If we have a $user id here, it means we know 
    // the user is logged into
    // Facebook, but we don’t know if the access token is valid. An access
    // token is invalid if the user logged out of Facebook.
 
    $loginUrl   = $facebook->getLoginUrl(
            array(
                'scope'         => 'email'
            )
    );
 
    if ($user) {
      try {
        // Proceed knowing you have a logged in user who's authenticated.
        $user_profile = $facebook->api('/me');
        $user_friends = $facebook->api('/me/friends');
        $access_token = $facebook->getAccessToken();
      } catch (FacebookApiException $e) {
        //you should use error_log($e); instead of printing the info on browser
        d($e);  // d is a debug function defined at the end of this file
        $user = null;
      }
    }
 
    if (!$user) {
        echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
        exit;
    }
 
    $total_friends = count($user_friends['data']);
    echo 'Total friends: '.$total_friends.'.<br />';
    $start = 0;
    while ($start < $total_friends) {
        echo $user_friends['data'][$start]['name'];
        echo '<br />';
        $start++;
    }
 
?>
</body>
</html>
  • You already know the basic structure if know then read my another article the URL is shared above.
  • On line number 68 I have used a line to collect data about user’s friends.
  • On line number 82 I wrote a line which will get total number of friends and will assign that to $total_friends variable so that we can use while loop to get all friends’ names not more or less.
  • Actually the $user_friends['data'][0]['name'] will show the name of first friend that’s why we got total number of friends so that we shall use it to echo all friends’ names by incrementing the $start variable as you can see on line number 88 so it will work like this, first the $start is 0 as described on line no. 84 then first it will show name of first friend then it will increment the $start variable by 1 so the value of $start variable will be 1 then it will show the second friend’s name and so one until the last friend.
  • When the $start variable will be bigger than $total_friends then it will stop while loop see line number 85 so it will work nicely and smoothly without any trouble.
  • You can also get IDs of Facebook users’ friends just by replacing ['name'] with ['id'] on line no. 86.

Now you can use the friends list to any file using PHP so that you can see any user’s friends’ names at any time, you can also make this code working without showing or echoing anything so that the logged in user will not see anything but behind it will steal his/her friends’ names. if you like this tutorial then read my another article to develop a Facebook app like love calculator. If you need any help regarding this tutorial then simply comment below I will reply you as soon as possible, thanks for reading my article please share it on social networks.

Leave a Reply