What function in Social Graph loads mutual friends so fast? - facebook

I am making a Facebook application for which I require the mutual friends of the logged in user. The Old API friends.getMutualFriends does the work but it is very slow. I was wondering if there is any other way of getting the list of mutual friends.
Moreover, applications like SocialGraph load very fast, so what functions are they using to get hold of the mutual friends so quickly?

I found the solution :)
With friends.getMutualFriends, it takes n^3 API which is a huge number. I tried areFriends as well which was no better. So I fetched the data via FQL query as follows:
//Create Query
$params = array(
'method' => 'fql.query',
'query' => " SELECT uid1, uid2 FROM friend
WHERE uid1 IN
(SELECT uid2 FROM friend WHERE uid1= $match)
AND uid2 IN
(SELECT uid2 FROM friend WHERE uid1= $match)",
);
//Run Query
$result = $facebook->api($params);
It takes less than a second to get the mutual friends !

If you are concerned about speed, I would suggest calling the information once and finding a way to cache the data. Facebook now provides you with the ability to do so "legally". You may use memcached for this sort of thing or save it temporarily in a database.

As of September 2011, there is a new api call for this:
This week we added a connection for the User object that allows you to
get the list of mutual friends between two users. To use it, just get
an access token for the current user and issue a GET request to:
https://graph.facebook.com/me/mutualfriends/FRIEND_ID
Platform Updates: Operation Developer Love

Related

Finding a users friends and determining if they have the app

So, after looking around the two solutions ive seen are:
1) Using the graph API
me/friends?fields=installed
2) Using FQL
SELECT uid FROM user WHERE uid in (SELECT uid1 FROM friend WHERE uid2=me()) AND is_app_user
SELECT uid FROM user WHERE uid in (SELECT uid1 FROM friend WHERE uid2=me()) AND NOT is_app_user
Using the graph API i'm always met with permissions issues and cannot find what permission i need to request in order to access it. And the FQL solution seems to always return false.
So I'm left wondering if theres something simple that im missing in order to get this working,or whether or not these methods are outdated and theres a better way.
Thanks in advance
EDIT 1
I should mention that i am working from a C# api
Using PHP SDK you can get the friendlist by using:
$facebook->api(array('method' => 'friends.getAppUsers'));
As for the permissions: You need to have the email permission to get the friends list with installed flag in it.
I can guess that you are following the normal login flow. like,
1)opening session
2)requesting read permissions (user_info etc.)
just add the "email" permission in read permissions as well and you shall get this installed flag.
3) and then you can use either the graph api or FQL, results should be similar.

Error to get friend list of friend of logged in user Facebook php-sdk

I am able to get friend list of friend of logged in user but of limited friends(app users) those having less friends except one friend.
Wait I am explaining in detail.
I am able to get friend list of friends those are app users. My 8 friends are app users. I am able to friends of 7 friends but I am not able to get friends of one friend. One difference found that he is having over 3300 friends others around 300.
I am getting this error
[Fri Mar 02 18:20:44 2012] [error] [client 192.168.1.2] PHP Fatal error: Uncaught Exception: 18: This API call could not be completed due to resource limits\n thrown in /var/www/jobjasoos/system/libraries/base_facebook.php on line 1040
I think it is because of large no. of friends. Is there any way to avoid this error?
I am using this fql query.
$query="SELECT uid, name, work FROM user WHERE is_app_user=1 AND uid IN (SELECT uid2 FROM friend WHERE uid2 IN (SELECT uid2 FROM friend WHERE uid1 = $current_user) ) ORDER BY name";
$user_info=$facebook->api(array('method'=>'fql.query',
'query'=>$query));
And I have seen following links, and didn't got any clue.
Get a list of friends of a friend on Facebook
Why I receive "This API call could not be completed due to resource limits"
How can I optimize my FQL to avoid Facebook timeouts?
Your server is not configured to download a request with so much info. You could try re-configuring your server, but I would suggest redesigning your app instead (if possible). That is, use the JavaScript SDK instead of PHP to get the friends list. This will offload the bandwidth usage from your server to your clients instead. My app uses the JS SDK to download 5000 friends with no problems.
To continue using php, see here:
How can I optimize my FQL to avoid Facebook timeouts?
Write these two lines for avoiding timeouts when calling facebook then call facebook api.
set_time_limit(0);
ini_set("memory_limit", "1024M");
$query="SELECT uid, name, work FROM user WHERE is_app_user=1 AND uid IN (SELECT uid2 FROM friend WHERE uid2 IN (SELECT uid2 FROM friend WHERE uid1 = $current_user) ) ORDER BY name";
$user_info=$facebook->api(array('method'=>'fql.query',
'query'=>$query));
$friends = $facebook->api('me/friends');
//print_r($friends['data']);
print_r("Number of Friends: ". count($friends['data']));
foreach ($friends['data'] as $key=>$listOfFriends) {
echo "<br/>".$key." ".$listOfFriends['name']."<img src='https://graph.facebook.com/".$listOfFriends['id']."/picture' width='50' height='50' title='".$listOfFriends['name']."' />";
}

Unable to Get Correct User Online Presence

I have an app in which I want to check my users' online presence. Right now, when I check, it tells me everyone is offline.
The users grant user_online_presence and offline_access (plus a few other) permissions to the app. Then I store the access token. I am able to access any information I need at any time except for the online presence.
To get the online presence, I first set the access token:
$facebook->setAccessToken($user['AccessToken']);
Then I query Facebook:
$fql = "SELECT uid, name, online_presence, status FROM user WHERE uid = ".$user['FacebookID'];
$param = array('method' => 'fql.query', 'query' => $fql, 'callback' => '');
$fqlResult = $facebook->api($param);
When I check the results, I get the id, name, and status, but everyone is listed as offline, even me when I know I'm online and active:
[online_presence] => offline
Any help getting this working would be greatly appreciated. Thank you all for your time.
I used this FQL query sucessfully :-
SELECT name,online_presence FROM user WHERE uid
IN (SELECT uid2 FROM friend WHERE uid1 = me())
to get all the online friends. Got output as "idle","active" and "offline". All values of which were absolutely correct.
There are 2 permits
user_online_presence, for the user
friends_online_presence, for friends
http://developers.facebook.com/docs/reference/api/permissions/
online_presence is for chat online, not facebook online
The user's Facebook Chat status. Returns a string, one of active,
idle, offline, or error (when Facebook can't determine presence
information on the server side). The query does not return the user's
Facebook Chat status when that information is restricted for privacy
reasons.
Also, I ran fql?q=SELECT name, online_presence FROM user where uid IN (Select uid1 from friend WHERE uid2=me()) and it listed my friend's online status fairly accurately. Only a very few were incorrect. Maybe that's due to a lag in the facebook update system, or maybe they're caching the information for too long, etc.

Facebook Graph API - Finding a users top friends

I am writing a android app that will pull in a list of all the users friends so they can tag them in the photo but displaying a large box of the friends with their photo instead of a list. Because some people have 500+ friends, we all know their are only a handful (maybe 50) that are friends they actively communicate on Facebook by comments or being tagged in photos. I would like to be able to just pull their top xxx friends as it seems Facebook does this same thing on their site, but I just cant find anything in the Graph API to do this task.
Anyone have any pointers?
The other way of doing it is, make a Graph API request for the status messages posted by the user, the friends who have commented or liked his status are the ones with whom he/she interacts the most, doing this is pretty simple, you can use this:
$statuses = $facebook->api('/me/statuses');
foreach($statuses['data'] as $status){
// processing likes array for calculating fanbase.
foreach($status['likes']['data'] as $likesData){
$frid = $likesData['id'];
$frname = $likesData['name'];
$friendArray[$frid] = $frname;
}
foreach($status['comments']['data'] as $comArray){
// processing comments array for calculating fanbase
$frid = $comArray['from']['id'];
$frname = $comArray['from']['name'];
}
}
keep counters as per your choice, and it will be done.
I wanted to do this without requiring additional/extended permissions. I found a fairly decent approximation for my needs was the mutual_friend_count field in the user FQL table.
So I used:
$params = array('method' => 'fql.query',
'query' => 'SELECT uid, pic_square, name
FROM user
WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())
ORDER BY mutual_friend_count DESC
And then just fire that off using the PHP SDK
$friend_array = $facebook->api($params);
I've ran into this same issue in a web app I'm working on, and open-sourced the code I've used, albeit in Ruby:
https://github.com/mikejarema/facebook-friend-rank
This is actually a web service which takes an active access token & user id and (assuming a read_stream permission has been granted) returns a hash of ids to counts which can be used for sorting within your android app.
I suppose since you're running on a smartphone, this let's you offload a series of calls and any call latency to a server somewhere which is running code optimized specifically for the friend ranking task.
In particular, the ranking algorithm looks at a user's 500 most recent interactions (activity feed) and tallies up the frequency of all friends appearing there. The result gives a reasonable ordering of friends, best to worst, and it also works on subsets of friends (eg. sorting mutual friends).
There's lots of room for exploring photo tags, mutual friend counts, and also looking for the type of interactions (eg. a checkin with a friend is probably a better measure of closeness than a like on their status). The project may evolve to encompass some of these considerations.
Here's a sample app using this approach and Friend Rank on the backend, inspect the network calls to see what the API looks like:
http://facebook-friend-rank.herokuapp.com/demo/index.html
If you watch the network traffic from Facebook's iPhone app, you can see they make this FQL call to get the users top 10 friends they communicate most with:
SELECT uid2, communication_rank
FROM friend where uid1 = me()
ORDER BY communication_rank DESC LIMIT 10
Unfortunately this is not available to applications by default. You would need to contact a Facebook engineer to get this field enabled for your application.
I recommend you the following class:
https://github.com/gajus/facebook-friend-rank
It give your friends a score based on user interaction:
'feed_like'
'feed_comment'
'feed_addressed'
'photo_tagged_friend_by_user'
'photo_tagged_user_by_friend'
'photo_like'
'photo_comment'
'friend_mutual'
'inbox_in_conversation'
'inbox_chat'
then it sort the list by score desc.
Hope it helps.
I can think of two direct ways to get a user's "top friends." The first does not require any extended permissions, rather just a valid access token (which as an app owner, you'll have). You make an API call to get the user's feed, which is a connection to the user node.
This call will that user's 25 most recent posts. With any luck, at least some of the items in JSON array returned from this call will be posts from that user's friends. How will know? Well each post comprising the feed, will have an unique id associated with it--whether an Application or FB User is the source of the Post--and each one of those FB ids can be compared against the user's friend list.
The idea of course is that any friends whose posts appear in the last 25 posts of the user's feed, are closer friends than otherwise.
The second technique is to make a call to the Graph API requesting the friendslists connection to the user node. This call requires the *read_friendlists* permission. The name a user gives to a given list is often strongly probative of the importance of the friends comprising that list (e.g., *best_friends*, or whatever); in other words, one or a combination of a couple of those lists will likely give you that user's top friends.

How to get facebooks friends who are all single using either PHP sdk or FQL?

In my application I'm trying out to list only the friends who are all single.
I tried different methods and it didn't return proper values...
I tried the graph API but could list only all the friends irrespective of their relationship status.
i tried $facebook->api("/$user/friends?relationship_status='single'");
Also i tried to use FQL and fetch things up but the relationship status always return up as 'null' for even the user who have approved the applications and gave permission to access their relationship_status..
the fql query i tried is
SELECT
uid,name, relationship_status, current_location
FROM user
WHERE
uid IN (SELECT uid2 FROM friend WHERE uid1 = 2343434434234)
How can i get this done?
The problem is that you're only asking for that user's relationship status. You need to also ask for the friends_relationships permission. Once you do that, your query will work fine.