Introduction
This article describes how to get your Facebook Friends list using a PHP script. Basically this articles tells you how to retrieve your Facebook Friends list and this application also calculates the total number of your Facebook Friends. To do that, use the following procedure.
Step 1
Go to https://developers.facebook.com/apps, and click on "Create New Application" that is at the right site.
After clicking on the image given below, the "dialog box" has been opened, you can provide the name of your application then click on the "Continue" button.
Step 2
After clicking on the Continue button, the new Captcha dialog box will be opened. Fill it in and click on the "Continue" button.
Step 3
After clicking on the Continue button, your app id and secret has been generated, as you will see in the following image:
Step 4
After Step 3, enter the name of the website that you want to share the messages and links of. You can also write the localhost (the id of your application that runs on the local server) with your PHP application name. The image given below shows how to do that. Then click on the "Save changes" button.
Step 5
After the completion of all these steps, download:
facebook-php-sdk-master.
It downloads as a zip file, extract it into your website root. Suppose I am using the "wamp" server, so I extract it into the www root.
Step 6
Now let's start working with the PHP script.
<html>
<body>
<?php
//facebook application configuration
$fbconfig['appid' ] = "write app id here";
$fbconfig['secret'] = "write app secret here";
try{
include_once ('.\facebook-php-sdk-master\src\facebook.php');
}
catch(Exception $o){
print_r($o);
}
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true,
));
$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'email'
)
);
if ($user) {
try {
$user_profile = $facebook->api('/me');
$user_friends = $facebook->api('/me/friends');
$access_token = $facebook->getAccessToken();
} catch (FacebookApiException $e) {
d($e);
$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>
Output