Ensure you have the latest SDK by going to https://github.com/facebook/facebook-php-sdk/zipball/master
Unzip and you have a layout as shown below
/facebook-php-sdk
index.php
Where index.php with the file being used to display in the browser the number of friends
Include the SDK at the start of the PHP file
require('sdk/src/facebook.php');
Go to https://developers.facebook.com/apps, select your app and get your App ID and App Secret, create an instance within the PHP file
$facebook = new Facebook(array(
'appId' => 'YOUR_APP_ID_HERE',
'secret' => 'YOUR_SECRET_HERE',
));
Then retrieve the $user
data so we know that the current user is authenticated
$user = $facebook->getUser();
Before sending any calls check whether the authentication was right
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
Now make a call to /me/statuses
the documentation is available at https://developers.facebook.com/docs/reference/api/user/#statuses
$statuses = $facebook->api('/me/statuses');
This should return an array of the structure Status message defined at http://developers.facebook.com/docs/reference/api/status/ of all the current user status messages.
Now you need to decide what determines top friends
- number of likes + comments
- number of comments
- number of likes
Let's choose option 1, and give each weight of 1. That is a like and a comment are equivalent in value for determining the amount of friends
So create a variable to hold this, for example $friendArray
Then make an iteration over all the status messages but the entire JSON reponse starts with a wrapped data
{
"data": [
So access $statuses['data']
, the foreach will give all status messages as a status
item
foreach($statuses['data'] as $status){
Within this loop iterate all the likes and increment the value of each id that appears
foreach($status['likes']['data'] as $likesData){
$frid = $likesData['id'];
$friendArray[$frid] = $friendArray[$frid] + 1;
}
Within this loop iterate all the comments and increment the value of each id that appears
foreach($status['comments']['data'] as $comArray){
$frid = $comArray['from']['id'];
$friendArray[$frid] = $friendArray[$frid] + 1;
}
At the end of the outer loop foreach($statuses['data'] as $status){
you should have the an array $friendArray
which has the scores.
Call asort
http://www.php.net/manual/en/function.asort.php to sort the array and you can then loop for the top x scores.
The code you showed isn't a function and is actually missing a closing brace, it does not actually return anything as it is not a function.
Caveats: The /me/statuses
only returns a limited set of status messages per call you need to get previous
page calls to iterate all your messages. The top friends returned are only based on the restriction I made up above.