Facebook FQL error when IN parameter exceeds 20 - facebook-graph-api

$fql = "SELECT link_id, owner, created_time, title, summary, url, image_urls FROM link WHERE owner IN (select uid2 from friend where uid1 = me() LIMIT 20) AND created_time >= $_7ago";
The above query works great. However, when I increase the LIMIT from 20 to, say 21 or anything higher, I get an error and the query returns Null. I am looking to query Links shared by ALL friends. Thanks.

That query takes forever to execute. Just testing it a few times in the Graph API explorer, even getting the links from 20 friends takes > 28 seconds. As I'm writing this, I've got a query running for 100 friends. We'll see if it completes before I do.
It looks like if you want to get all the links shared by all friends, you'll need to break this query up into smaller chunks and repeat it multiple times.
I might structure it like this to get your closest friend's links first, and then dig deeper for people who have less in common with you:
SELECT link_id, owner, created_time, title, summary, url, image_urls FROM link
WHERE owner IN (SELECT uid FROM user WHERE uid IN
(SELECT uid2 FROM friend WHERE uid1 = me())
ORDER BY mutual_friend_count LIMIT $offset, 20)
AND created_time >= $_7ago
You'd increment $offset in multiples of 20.
This is best done as an asynchronous query where you'll get the initial results quickly and others will fill in as they become available. It's going to take a LONG time to get all these results.
(100 Friends query still hasn't returned results after 5 mins.)
BTW, don't forget to mark your questions as resolved.

Related

Getting all links of any sort shared by a user's facebook friends

I am trying to get all links shared by a user's friends I tried all of the queries and requests from the below questions,
How do I get a friend's links with Facebook's graph api?
Finding who shared a link publicly and finding who reshared a given link
Facebook API: All links shared by all friends?
get shared links on facebook newsfeed
but unable to figure out what would be the exact FQL or the request URL to get all links shared from my friends from a defined past interval. I am kind of a newbie with the graph API and FQL, so kindly help me in figuring out a way to do this.
I tried a simple query
SELECT link_id, owner, owner_comment, created_time, title, summary, url, image_urls FROM link WHERE owner IN (SELECT uid2 FROM friend WHERE uid1 = me() LIMIT 100)
but I get
{
"error": "Request failed"
}
as the response from the API Explorer.
Your query is too long for FQL to construct the answer. If you'll try this:
SELECT link_id, owner, owner_comment, created_time, title, summary, url, image_urls
FROM link WHERE owner IN
(SELECT uid2 FROM friend WHERE uid1 = me() LIMIT 10)
(the same query, only limit set to 10 instead of 100) FQL explorer will think ~5-8secs but then give you back the result. Or if you will not receive the result, you have to reduce limit again.
So you have 2 choices:
You can lower you limit from 100 to 10 or less and run your query without changes
You can run query
SELECT uid2 FROM friend WHERE uid1 = me()
and then for each received friend ID run another query
SELECT link_id, owner, owner_comment, created_time, title, summary, url, image_urls
FROM link WHERE owner = FRIEND_ID

How can I use FQL to count the number of specific pages each friend likes?

Here is an example query:
SELECT uid FROM page_fan WHERE page_id IN (104266592953439, 16155433, 5768707450)
AND uid IN (SELECT uid2 FROM friend WHERE uid1=me())
This will give me the uids of friends who like either of the above three pages.
I would like a solution that lets me know how many of the pages within the above subset is liked by each friend (provided they like at least one of them). Something that can scale out to 20+ pages within the subset would be ideal.
For example, if a friend likes all 3 of the pages, listing their uid 3 times would suffice. Although I'm not sure if that is possible in FQL.
Using PHP Facebook SDK.
The Graph API doesn't have a count method, so you're going to have to do this in your script.
Change your query above to this:
SELECT uid, page_id FROM page_fan WHERE page_id IN (104266592953439, 16155433, 5768707450)
AND uid IN (SELECT uid2 FROM friend WHERE uid1=me()) ORDER BY uid
Then loop through your result and count how many times each uid appears.

Facebook API get shared friends likes with same app installed

I need to get in some way the shared likes between friends using the same app, some FQL that finally return to me a list of most common likes, is it possible?
Thanks.
I've had a look at this, and to get the likes for a current user based off the indexable user_id field, you need a user_access token for the current user.
https://developers.facebook.com/docs/reference/fql/like/
What this basically means is,only the current user of your app at a particular time can query information based on their likes. Something like the following query might work for you. Be aware that you need the read_stream permission
select object_id, user_id,object_type FROM like WHERE object_id in(SELECT object_id FROM like WHERE user_id =me()) and user_id in(select uid FROM user WHERE uid in(SELECT uid2 FROM friend where uid1 = me()) and is_app_user='true')
you can try this out at
https://developers.facebook.com/tools/explorer/433871385166/?fql=select%20object_id%2C%20user_id%2Cobject_type%20FROM%20like%20WHERE%20object_id%20in(SELECT%20object_id%20FROM%20like%20WHERE%20user_id%20%3Dme())%20and%20user_id%20in(select%20uid%20FROM%20user%20WHERE%20uid%20in(SELECT%20uid2%20FROM%20friend%20where%20uid1%20%3D%20me())%20and%20is_app_user%3D'true')
You would then most likely need to use some server side code to analyse the returned data to count what is most popular. Possibly by creating an array, looping through the data, check if there is a key in the array based off the object_id, if there isn't add that key to the array with a value of 1 (where 1 is the count) otherwise increase the value by 1.
pseudo code example
$data; // This would be what was returned from your FQL query
$compare = array();
foreach($data as $value){
if(array_key_exists($value['object_id'],$compare)){
$compare['object_id'] = $value['object_id'] + 1;
}else{
$compare['object_id'] = 1;
}
}
// Do some sorting function to compare the counts.
//you would then probably need to batch queries to get the name/title of the object that the user has liked
I hope this gives you a good starting point

how to find friend's past checkins near a location?

In my app, a user is at a location and is looking for her friends who have been anywhere withing 10 miles of where she is. How do I find this with either FQL or graph? The only way that I can see is by running a search like so: https://graph.facebook.com/search?type=checkin and then running through the results to find out which location was within 10 miles. Is there a better way for this?
Thanks for your help!
Doles
From http://developers.facebook.com/docs/reference/fql/location_post/
It says
An FQL table that returns Posts that have locations associated with
them and that satisfy at least one of the following conditions:
you were tagged in the Post
a friend was tagged in the Post
you authored the Post
a friend authored the Post
Note: This query can process a large amount of data. In order to
ensure that a manageable amount of data is returned within a
reasonable timeframe, you should specify a recent timestamp to narrow
the results.
Return posts within 10,000 meters of a given location:
SELECT id, page_id
FROM location_post
WHERE distance(latitude, longitude, '37.86564', '-122.25061') < 10000
Although the initial answer did work for me for part of my purpose, it quickly became inadequate. Now, after banging my head against the wall, it finally broke (not my head - the wall). Here are two more BETTER ways that WORK to find what I need:
This is to find just checkins:
SELECT checkin_id, coords, tagged_uids, page_id FROM checkin WHERE
(author_uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) or
author_uid=me()) and coords.latitude<'45.0' and coords.latitude>'29'
and coords.longitude>'-175' and coords.longitude<'-5';
This is to find all location posts:
SELECT id, page_id FROM location_post WHERE (author_uid IN (SELECT
uid2 FROM friend WHERE uid1 = me()) or author_uid=me()) and
coords.latitude<'45.0' and coords.latitude>'29' and
coords.longitude>'-175' and coords.longitude<'-5'

Facebook Graph API: FQL Requests Older Than 30 Days

I'm a bit confused.
The docs say:
Each query of the stream table is limited to the previous 30 days or
50 posts, whichever is greater, however you can use time-specific
fields such as created_time along with FQL operators (such as < or >)
to retrieve a much greater range of posts.
So I tried, for example:
NSString *fqlStr1 = [NSString stringWithFormat:
#"SELECT actor_id, message FROM stream WHERE source_id = me() AND created_time > %d AND created_time < %d",dayBefore,dayAfter];
This was meant to display all posts between the day before and the day after my birthday...
Which sorta worked..but it only was able to grab like 8 posts... and the posix dates are correct I checked them in a calculator..and its weird when I adjust the times a bit..like making the dayBefore earlier and the dayAfter later...I get a different set of posts but still only like 10....should I do a multiquery in like...1 hour intervals? Whats the reason for this?
-Josh
Try adding LIMIT 5000 to the end of your query.