Flash. Objective-C. Mobile. Tablets.

I was looking for a solution to analyze my facebook friends and then tell me which are the friends that has most mutual friends.

First attempt

I found a working solution from an answer in stackoverflow here.

Basically the FQL command is

SELECT uid1,uid2 FROM friend
WHERE uid1 IN
(SELECT uid2 FROM friend WHERE uid1=me())
 AND uid2 IN
 (SELECT uid2 FROM friend WHERE uid1=me())

But this command actually returns us information like this:

frenA, frenB
frenA, frenC
frenA, frenD
frenE, frenF

You have to do some counting in php to realise that frenA has three mutual friends with you. Then eventually you will get the friends with top mutual friends.

This is tedious, and I noticed that the result is inaccurate, as I have seen somewhere mentioned, FQL response is limited (4000 or 5000), and this FQL returns too much data and therefore it will be TRUNCATED.

Furthermore, there is no convenient way to return the name of the user in one single call.

Second Attempt

Then, I realize there is a “mutual_friend_count” in the user table. The FQL call to achieve the same target should be

SELECT name,mutual_friend_count FROM user WHERE uid IN(
SELECT uid2 FROM friend WHERE uid1=me())

This will return much lesser data, plus, you get the names!

Here is a piece of php code that you can try it out immediately. Just modify the app id and app secret etc and it will work.


<?php

session_start();

$app_id = "YOUR_APP_ID";
$app_secret = "YOUR APP SECRET";
$my_url = "URL TO THIS PHP";
$code = $_REQUEST["code"];

function cmp($a, $b)
{
 return $b['mutual_friend_count'] - $a['mutual_friend_count'];
}

if(empty($code))
{
 $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
 $dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
 . $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
 . $_SESSION['state'] . "&scope=rsvp_event";

echo("<script> top.location.href='" . $dialog_url . "'</script>");
}

if($_REQUEST['state'] == $_SESSION['state'])
{
 $token_url = "https://graph.facebook.com/oauth/access_token?"
 . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
 . "&client_secret=" . $app_secret . "&code=" . $code;

$response = @file_get_contents($token_url);
 $params = null;
 parse_str($response, $params);

 $token = $params['access_token'];

 $jsonurl = "https://api.facebook.com/method/fql.query?query=SELECT%20name%2Cmutual_friend_count%20FROM%20user%20WHERE%20uid%20IN(%0ASELECT%20uid2%20FROM%20friend%20WHERE%20uid1%3Dme())&access_token=$token&format=json";

 $json = file_get_contents($jsonurl,0,null,null);
 $json_output = json_decode($json,true);

 usort($json_output, "cmp");

 foreach ($json_output as $key => $value)
 {
 echo $value["name"] . ":" . $value["mutual_friend_count"] . "<br>";
 }
}
else
{
 echo("The state does not match. You may be a victim of CSRF.");
}

?>

About these ads

Comments on: "PHP to get highest mutual friends count in facebook" (1)

  1. awesome worked like a charm thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: