How can I count the total friends of a Facebook user by uid? - facebook

How can I count the total friends of a Facebook user by uid?

There is a friend_count field against the user table that you can query.
SELECT friend_count FROM user WHERE uid = me()
Just replace me() with the UID of the user that you want to query.

#Scott
This very much works, I didn't expect it to :)
https://graph.facebook.com/fql?q=SELECT friend_count FROM user WHERE uid = 273103345
I tried with a random user id who is not a friend in Graph API. However for few random uids it returned null, I am guessing they were pages.

FQL is now deprecated as of October 2014. Quoting Facebook's FAQ :
As of Tuesday 22nd July 2014, we now return a total_count field within
a summary property in the response to /v2.2/me/friends.
The user/friends endpoint does require the user_friends permission to return an actual list of friends who use your app, however the friend count field will still be returned without this the user_friends permission granted. Sample response:
{
"data": [
],
"summary": {
"total_count": 811
}
}
Full FAQ: https://developers.facebook.com/docs/apps/faq#total_friend_count

COUNT is not supported by FQL:
SELECT uid2
FROM friend
WHERE uid1 = USERID
Since COUNT is not supported you need to get all of the records and then count them in whatever application that is making the SQL call.
If you want to get more information, like names, along with count you can use an inner select to get the friend's extended info:
SELECT name
FROM user
WHERE uid IN (SELECT uid2
FROM friend
WHERE uid1 = USERID)
Here is a related SO question:
FQL result count
References for Friend table:
Query this table to determine whether
two users are linked together as
friends.

Heres my solution... works great!
$myfriends = $facebook->api('/XXXXXXX152466/friends');
$friendcount = COUNT($myfriends['data']) + 1;
Replace XXXXXXX152466 with the facebook profile id#
Using Facebook PHP SDK along with Graph API!

Starting with Facebook API 2.0 you can't get the full list of Facebook friends, only a partial list of friends that also authorized your app.
You can still get the total number of friends though, the Graph API user/friends has a field summary containing the count as total_count
So you can get it using:
FB.api("/me/friends", function (response) {
if (response && !response.error) {
console.log(response.summary.total_count);
}
});

There are lot of way to count the no of friends using Facebook Graph API with Php SDK, out of one i am telling..
$fbuser=$fb->api('/me/friends');
$access_token = $fb->getAccessToken();
$friend_count=0;
foreach($fbuser['data'] as $friends)
{
$friends_count++;
}
For complete tutorial visit this article Getting FB Friends Count

Related

How to filter user's news feed to posts/actions from a specific app in Graph API

I have a Facebook app and I want to filter user's news feed to get latest posts/actions that was posted using my app, in Graph API.
I've found this: How to get activity from my app with Facebook Graph API?
This one is okay for getting actions/posts by a specific friend (or the user itself), what I need is almost the same, with the difference that I need this "stream" by all friends of the user that use my app, not a single user.
Then I've found this: Facebook graph api : how to filter app feeds. For this one, the example in the question itself returns empty data for me (with the appropriate access token with read_stream permission). The only answer suggests to use FQL. I've tried SELECT post_id, message, comments, likes FROM stream WHERE app_id = MY_APP_ID and I got this error:
{
"error": {
"message": "Your statement is not indexable. The WHERE clause must contain an
indexable column. Such columns are marked with * in the tables linked from
http://developers.facebook.com/docs/reference/fql ",
"type": "NoIndexFunctionException",
"code": 604
}
}
How do I retrieve the list of all the last (say 100) posts from a specific app by the user and their friends, from the user's scope?
With all FQL queries, your WHERE clause must include at least one indexable field (those are marked with a star next to the field name in the docs) – for performance reasons.
So for the stream table you have a choice between source_id (id of the user whose wall the post is on) or filter_key. You can read the valid filter keys from the stream_filter table – but every user has the filter key nf for what shows up in their news feed. (post_id is also indexable, but of no use for us here.)
So you could try filtering with WHERE filter_key = 'nf' AND app_id = 'YOUR_APP_ID' – that should give you the posts that your active user is able to see in their feed that where made via your app.
You could also try with source_id, filtering that to either being equal to me() (your current user in FQL), or it being in the list of your friends (whose ids you can get from the friend table, again using me(), as a sub-query) - so like this,
… WHERE (source_id = me() OR source_id IN (SELECT uid1 FROM friend WHERE uid2 = me())) AND app_id = 'YOUR_APP_ID'
– but I think that will have less good performance, so I’d try with filter key first and see if that gets you the posts you want.
EDIT: So, as the questioner found out, using /me/home?filter=app_MY_APP_ID works apparently better, and is also easier. Whoever else might need this, they should use this version, since it is more reliable – FQL often gives less results then one would expect.

Get Friends who have liked a particular page

I am using JavaScript to retrieve friends's info as follows:
FB.api('/me/friends', {fields:'name,id,hometown,education'}, function(response){
var l=''
$.each(response.data,function(idx,val){
l=l+val.id+(idx<response.data.length-1?',':'')
});
FB.api("likes?ids="+l,function(res){
allLikes = res;
});
}
Using the above code, I have successfully retrieved all likes of all of the user's friends. I wanted to check which of the user's friends have liked a particular page. The response data is about 1.5MBs. I have to iterate through individual likes of all of the friends one-by-one to figure this out. How to retrieve info only those friends who have already liked a particular page?
You can get all the user ids of the user's friend who have liked a particular <page_id> using following FQL query.
FB.api({
method: 'fql.query',
query: 'select uid from page_fan where page_id=<page_id>
and uid in (select uid2 from friend where uid1=me())'
},function(response){
//do what you want to do with user ids (uid) you got
});

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.

I'm getting "The indexed user_id queried on must be the logged in user" when i query my friends likes

here is my query:
FB.api(
{
method: 'fql.multiquery',
queries: {
'friends':'SELECT uid1 FROM friend WHERE uid2= me()',
'likes':'SELECT object_id FROM like WHERE user_id in (SELECT uid1 FROM #friends)'
}
});
Some FQL queries require that the queried user be logged in. In order to run theis queries without the user being logged in, your app must have the offline_access permission.
indexed fields cannot be queried except actual user
for query your friends likes look at How to get friends likes via Facebook API

Facebook Graph API + Facebook Pages

Using Facebook's Graph API, given a username xyz (assuming they've authenticated my site), how do I get a list of all of the facebook pages that the user administers?
The accounts property on the user object says:
The Facebook pages owned by the current user. If the manage_pages permission has been granted, this connection also yields access_tokens that can be used to query the Graph API on behalf of the page.
http://developers.facebook.com/docs/reference/api/user
After getting access token you can get all details of list of all of the facebook pages that the user administers.
https://graph.facebook.com/[FACEBOOKUSERID]?metadata=1&access_token=
The output will look like
{
"name": "Facebook Developer Garage Austin - SXSW Edition",
"metadata": {
"connections": {
"feed": "http://graph.facebook.com/331218348435/feed",
"picture": "https://graph.facebook.com/331218348435/picture",
"invited": "https://graph.facebook.com/331218348435/invited",
"attending": "https://graph.facebook.com/331218348435/attending",
"maybe": "https://graph.facebook.com/331218348435/maybe",
"noreply": "https://graph.facebook.com/331218348435/noreply",
"declined": "https://graph.facebook.com/331218348435/declined"
}
}
}
Use an fql query! That is the best way to get the pages for which the user is admin. You will also be able to restrict the names also. i.e pages with empty names A sample fql query which gives you the page details as well.
SELECT page_id,page_url,name,pic_square FROM page WHERE page_id IN (SELECT page_id FROM page_admin WHERE uid = " + **UserId** + ") and name!=''
UserId -- Id of the admin
NOTE
This method will not work from version 2.1 of graph api as fql was deprecated from that version onwards
I found the answer, you need to use FQL, passing in the appropriate access_token:
https://api.facebook.com/method/fql.query?query=SELECT%20page_id%20FROM%20page_admin%20WHERE%20uid=XXXX&access_token=YYYY
Here's what I use. It works perfect
$pages = $facebook->api(array('method' => 'fql.query','query' => 'SELECT page_id FROM page_admin WHERE uid = '.$uid.''));
foreach($pages as $k=>$v) {
echo 'page id#:'.$v['page_id'].'<br/>';
}
This is of course after you have the session created for the fb user! The $uid would be the profile id# of the specific facebook user your returning of a list of managed pages for.
#rmorrison - This is not graph API though. With the new "like" addition, you can use this URL: h ttps://graph.facebook.com/me/likes?access_token=blah! or h ttps://graph.facebook.com/USER_ID/likes?access_token=blah!
Fql is the best way to get the pages of the user for which he is the admin. As the others like likes of user will give all the pages that the user like.
You can get list of pages from a simple Facebook Graph API Hit.
https://graph.facebook.com/{user-id}/accounts?access_token={access-token}
It will give a list of pages for user which he/she has created.
access_token is valid for an hour or less. What should someone do in order to get the details even after. I was trying to result get the result from
https://graph.facebook.com/v2.3/search?q=coffee&type=place&center=37.76,-122.427&distance=1000&access_token=xyz
This worked for an hour or so but how do i get the result after an hour...without going for a login.