FQL Query to get people who like a page - facebook

I am new to Facebook development and I am playing around with some proof of concepts. How would I write a query to get a list of all my friends that like the U2 page?
I am thinking something like this.
var result = fb.Get("fql", new { q = "SELECT name FROM user WHERE uid IN (SELECT uid1 FROM friend WHERE uid2=me()) AND 'U2' IN like" });
also checkins
var result = fb.Get("fql", new { q = "SELECT checkin_id, author_uid, page_id FROM checkin WHERE author_uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND page_id IN (SELECT page_id FROM page WHERE name = 'MyPlace')" });
Also is there a query builder tool?

This should do it (example is with the PHP SDK (v3.x) )
$page_id = ID OF PAGE
$result = $facebook->api(array(
'method' => 'fql.multiquery',
'queries' => array(
'friends_liking_page' => 'SELECT uid, page_id FROM page_fan WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND page_id = ' . $page_id,
'result' => 'SELECT uid, name, pic FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me() AND (uid2 IN (SELECT uid FROM #friends_liking_page)))'
)
));
you'll need user_likes and friends_likes permission

Related

Facebook page_fan returning NULL, can't check if user likes page

I'm working on a Facebook app and I need to know if the user likes a specific page or not.
I tried using:
$isFan = $facebook->api(array(
"method" => "fql.query",
"query" => "SELECT uid FROM page_fan WHERE page_id = 'SOMEPAGEID'
AND uid = me()"
));
It returns NULL.
I even tried the javascript SDK:
FB.api('/me/likes/SOMEPAGEID', function(response) {
console.log(response.data);
});
It returns nothing.
I'm asking for the following permissions:
$scope = "email,user_likes";
Am I missing something?
Edit: SELECT * was bad copy-paste... it's SELECT uid
Your permissions look correct but I don't think that FQL queries actually support SELECT *, you have to manually list the fields
So try changing your FQL query to
select page_id from page_fan WHERE uid = me() AND page_id = PAGE_ID
Or it might be you haven't encoded your query properly. This runs for me in the graph explorer https://developers.facebook.com/tools/explorer?method=GET&path=fql%3Fq%3Dselect%2Bpage_id%2Bfrom%2Bpage_fan%2BWHERE%2Buid%2B%253D%2Bme%2528%2529
fql?q=select+page_id+from+page_fan+WHERE+uid+%3D+me%28%29
so try using
$isFan = $facebook->api('/fql?q='.urlencode('select page_id from page_fan WHERE uid = me() AND page_id = PAGE_ID'));

Retrieve just the like_count from the like_info of a Photo

I would like to ask how to retrieve just the like_count from this query?
https://graph.facebook.com/fql?q=SELECT%20like_info%20FROM%20photo%20WHERE%20object_id=447876785249920
The result is
{"data": [
{
"like_info": {
"can_like": false,
"like_count": 2318,
"user_likes": false
}
}
]
}
I just want to have the like_count for that particular photo.
You can use it in the query as:
1. SELECT uid2 FROM friend WHERE uid1 = me()
2. SELECT uid FROM page_fan WHERE page_id='PAGE_ID' AND uid IN (
SELECT uid2 FROM friend WHERE uid1 = me()
)
3. SELECT first_name, last_name, pic_big FROM USER WHERE uid IN(
SELECT uid FROM page_fan WHERE page_id='PAGE_ID' AND uid IN (
SELECT uid2 FROM friend WHERE uid1 = me()
)
)
for more details here is the link:
http://www.leftjoin.net/2011/03/getting-list-of-user-friends-who-like-some-page/
FQL query to select likes count
SELECT like_info.like_count FROM photo WHERE object_id='your photo id'

facebook upcoming birthday fetching only this year

i want to fetch Facebook friend birthdays from current date to next year current day . todate is the date 364 days later to now in m/d. for this purpose first fql is to retrieve data from jan to current date and second one is for fetching data from current date to 31 dec. i want to apply pagination to upcoming birthdays for next 11 month and 29 days.the following queries are working and fetching the data.can anyone tell me the best way to do so. any suggestion will be highly appreciated.
$fromDate = date('m/d');
$past_birthdays = $facebook->api(array(
'method' => 'fql.query',
'query' => "SELECT uid, name, birthday, birthday_date,substr(birthday_date,0,3) FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND birthday_date<'$toDate' AND birthday_date>='01/01' order by birthday_date",
'access_token' => $accessToken
));
if ($toDate > $fromDate) {
$condition = " AND birthday_date<'$toDate'";
} else {
$condition = "";
}
$upcoming_birthdays = $facebook->api(array(
'method' => 'fql.query',
'query' => "SELECT uid, name, birthday, birthday_date,substr(birthday_date,0,3) FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND birthday_date>='$fromDate' $condition order by birthday_date",
'access_token' => $accessToken
));
as in your case i checked on facebook api explorer if you provide limit in your query, then its output in not consistent , the best way to solve your problem is load the whole birthday
list in array and show it in slots.
$fromDate = date('m/d');
$past_birthdays = "";
$upcoming_birthdays = $facebook->api(array(
'method' => 'fql.query',
'query' => "SELECT uid, name, birthday, birthday_date, substr(birthday_date, 0,3) from user where uid in (select uid2 from friend where uid1=me()) and birthday_date >='$fromDate' order by birthday_date Limit 0,500",
'access_token' => $accessToken
));
$no = count($upcoming_birthdays); //exit;
if ($no < 500) {
$Limit2 = 500 - $no;
$past_birthdays = $facebook->api(array(
'method' => 'fql.query',
'query' => "SELECT uid, name, birthday, birthday_date, substr(birthday_date, 0,3) from user where uid in (select uid2 from friend where uid1=me()) and birthday_date >='01/01' and birthday_date <= '$fromDate' order by birthday_date Limit 0,$Limit2",
'access_token' => $accessToken
));
//echo count($past_birthdays);exit;
$upcoming_birthdays = array_merge($upcoming_birthdays, $past_birthdays);
no need to take todate as you are fetching for whole year.this ccode is to fetch only 500 but run query for whole year.

fql query fails - suspected auth issue

In a page, if I make a test fql query it works:
$fql = 'SELECT name from user where uid = ' . $user_id;
$ret_obj = $facebook->api(array(
'method' => 'fql.query',
'query' => $fql,
));
echo '<pre>Name: ' . $ret_obj[0]['name'] . '</pre>';
However if I try to run this query, it fails:
$fql2 = 'SELECT uid, name, pic_square FROM user WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me()) AND sex =male';
$ret_obj2 = $facebook->api(array(
'method' => 'fql.query',
'query' => $fql2,
));
echo '<pre>Name: ' . $ret_obj2[0]['name'] . '</pre>';
I suspect it is an auth problem, because the same fql query works in the graph api test tool!
If you want to checkout the full php code, it's the second example on this page:
https://developers.facebook.com/docs/reference/php/facebook-api/
You need just basic permissions for this query, so this is not the issue. Try adding double quotes around "male":
$fql2 = 'SELECT uid, name, pic_square FROM user WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me()) AND sex="male"';
Thanks for the support guys, the query wasn't working for 2 reasons:
1) I forgot the ; after the end of statement
2) In my code I used
`SELECT uid, name, pic_square, FROM`
instead of
`SELECT uid, name, pic_square FROM`

FQL page_fan query to filter results of only one page

I have a fql query like this:
SELECT uid,page_id,type,profile_section,created_time FROM page_fan WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) ;
Is there any way to get only the results where page_id = XXXXXX?
Because the page_id is not indexable.. and i was hoping for a way to filter it.
I guess i found a great solution that works for me to get friends that are not pagefans.
$facebook_result = $this->facebook->api(array(
'method' => 'fql.multiquery',
'queries' => array(
'friends_liking_page' => 'SELECT uid, page_id FROM page_fan WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND page_id = ' . $page_id,
'result' => 'SELECT uid, name, pic FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me() AND NOT (uid2 IN (SELECT uid FROM #friends_liking_page)))'
)
));