Facebook FQL Get All Posts At Location - facebook

The location_post FQL table is described as the following:
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
So I wrote the following FQL script with the intent of getting all posts my friends and I had made at a certain location:
SELECT id, message, post_id, timestamp FROM location_post WHERE page_id=303736809701751
However, an empty data set is returned. In this particular case, I expect some of my own posts to be returned, though I have tried other place ids that should have returned friends' posts as well.
What is missing from the request, or is this a bug I should report to Facebook?

The paragraph you quoted from the doc just says that the table only contains places where yourself and friends of yours have been. It tries to explain that you will not find public users there.
A query to the location_post table does not automatically specify the concerned IDs for you. If you want to search for the places where all your friends have been, you will have to indicate all your friends. If you are only interested by a bunch of friends, just add the concerned IDs.
This should do it.
SELECT id, message, post_id, timestamp
FROM location_post
WHERE (author_uid IN (SELECT uid2 FROM friend WHERE uid1 = me())
OR author_uid = me()
OR tagged_uids IN (SELECT uid2 FROM friend WHERE uid1 = me())
OR tagged_uids = me())
AND page_id = 303736809701751

Related

FQL query limit

I was trying this simple query on the Graph Explorer
SELECT eid FROM event_member
WHERE (uid IN (SELECT uid2 FROM friend WHERE uid1 = me())
OR uid = me())
and it returns "error: Request Failed."
Then, I started reading here and some forums and it had to do with the limits, so I've tried limiting by 20 my friend list and it worked.
Which is the limit? and where (the query, or the results given)? There's nothing in the documentation as far as I know.
EDIT:
What if I order my whole friends by mutual_friend_count and then limit by 200? I also added start_time>now(). It's not the best way to find the closest friends, but it doesn't need permissions to access your stream (to check likes and comments from friends).
SELECT eid FROM event_member
WHERE (uid = me()
OR uid IN (SELECT uid FROM user
WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()
ORDER BY mutual_friend_count DESC)
LIMIT 200)
)
AND start_time > now()
Is it too many queries inside another one? They are three actually.
It isn't efficent
in some cases. For example, in my case, the top mutual friends lives
in my hometown, and I'm interested in events from my actual city.
Imagine I limit the events retrieved by only 5 per user. It shouldn't be so hard ONLY IF the user had a list of upcoming events he/she is attending/unsure.
For example, doing graph.facebook.com/user-id/events "seems" to work like that.
However, I guess reading from event_member table it's not so easy because it would traversing a large database of events and checking one by one if any friend of mine is in there. Am I wrong?
The limits are as far as I know (and you wrote it as well) not documented. I'd recommend that you use the
LIMIT start, end
in your subquery as follows
SELECT eid FROM event_member
WHERE (uid IN (SELECT uid2 FROM friend WHERE uid1 = me() LIMIT 0,24)
OR uid = me())
A best practice that I use is to get the number or friends in advance, and then create "breakdown queries", i.e. you've got 187 friends and the desired limit is 25 per query, then you'll need to 8 "breakdown queries" to get the whole event list from your friends. This can also be done via a batch request (but friends need to be counted first in a separate query).
The general limit of these calls on Facebook's side are either the processing time or the length of the result I guess...

Getting list of my friends that are tagged in a photo

Users, events and groups can all be tagged in a photo. In the available fields from photo_tag, you can't distinguish what subject id is a user, event or group. I want to just return a list of my friends that are tagged in a photo, ignoring events, groups and users that aren't my friend.
Does anyone know why this returns 0 results?
SELECT subject, xcoord, ycoord, text
FROM photo_tag
WHERE object_id = [PHOTO_OBJECT_ID]
AND subject IN (SELECT uid2 FROM friend WHERE uid1 = me())
Even though...
SELECT subject, xcoord, ycoord, text
FROM photo_tag
WHERE object_id = [PHOTO_OBJECT_ID]
Returns a list of users which includes friends of mine, verified by...
SELECT uid2 FROM friend where uid1 = me() and uid2 = [SUBJECT_USER_ID]
Apparently this is "by design"!
Facebook's response:
This is by design, after you have the list of subjects, you can query the user table to see if that subject is a user.
https://developers.facebook.com/bugs/549553255066877

FQL Get all of my friends' checkins?

This seems to be impossible as of now .. can anyone give me a suitable query or a set of queries that can do this? Basically, I need ALL my friends' checkins: Ones they've checked in by themselves, Ones in which they've checked with a picture, Ones in which a user who is not my friend tagged them in.
My first attempt was this:
SELECT checkin_id, author_uid, page_id, coords, tagged_uids, timestamp FROM checkin WHERE (author_uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) OR author_uid=me()) ORDER BY timestamp DESC LIMIT 50
But this doesn't include checkins with pictures.
My second attempt was this:
SELECT id, author_uid, app_id, timestamp, tagged_uids, page_id, page_type, coords, type FROM location_post
WHERE author_uid IN (SELECT uid2 FROM friend WHERE uid1=me())
But like the WHERE clause suggests, we're only looking for checkins which were created by a friend, but not ones in which my friends are tagged.
My next attempt was this:
SELECT id, author_uid, app_id, timestamp, tagged_uids, page_id, page_type, coords, type FROM location_post
WHERE tagged_uids IN (SELECT uid2 FROM friend WHERE uid1=me())
But it just returns an empty data set as it probably should.
So short of submitting 500 odd queries, one for each of my friends, is there a way to do this?
Any pointers are much appreciated!
Thanks,
Teja
we can also use following FQL to get friends checkins and checkins in which friends are tagged:
SELECT checkin_id FROM checkin WHERE author_uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) OR tagged_uids IN (SELECT uid2 FROM friend WHERE uid1 = me())
Does this need to be in FQL or would the Graph API not suffice for this?
/me/friends?fields=checkins, although very slow (warning: with lots of friends this could timeout completely), returns me a list of my friends with an array of their recent checkins (and those checkins' likes, comments, etc)
You could also break it up into batches of friends for better performance:
/?fields=checkins&ids=[CSV LIST OF FRIENDS TO CHECK]
I was recently trying to solve the same problem and finding that using the graph via /me/friends?fields=checkins was too slow to be usable and sometimes times out completely.
I did just stumble across the search function though, which seems to do exactly what I (and perhaps you) want.
To quote the facebook documentation:
Checkins: https://graph.facebook.com/search?type=checkin (This request
returns you or your friend's latest checkins, or checkins where you or
your friends have been tagged; currently, it does not accept a q=
parameter.)
There's tons more examples here under Search: https://developers.facebook.com/docs/reference/api/

Get latitude/longitude of friend's current_location using Graph FQL

I'm trying to get the latitude/longitude of all of a user's friends using a single API call. I believe I need to write a multi-query FQL statement, but I can't get the syntax correct.
I believe the two queries need to be something like the following:
'"friends":"SELECT uid,current_location FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())"'
'"location":"SELECT location FROM page WHERE id IN (SELECT current_location_id FROM #friends)"';
My problem is that somehow I need to get the ID of the current_location in the first query so that I can reference it in the second query, but I only know how to get an array that contains the id.
Thanks for any help!
Clarification:
I'm not trying to get check-ins of friends. Instead I want to graph the "current_location" (i.e. where they live) of all of a user's friends on a Geo map. I can get a user's "current_location" with the following FQL query:
"SELECT uid,sex,current_location FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())"
This query returns the following array:
[current_location] => Array
(
[city] => New York
[state] => New York
[country] => United States
[zip] =>
[id] => 108424279189115
[name] => New York, New York
)
Which does not contain the longitude and latitude of the of the current_location (New York in the above example)
Therefore I now need to take the ID of the current_location (e.g. 108424279189115) and run a second FQL query such as:
"SELECT location,name FROM page WHERE page_id = 108424279189115)"
To get the latitude and longitude of the current_location. Currently, I have a working code that runs the first FQL query, then extracts page_id in php, and then runs a second FQL query to get the current_location latitude/longitude. However, for performance reasons I would like to run only one multi-query if this is possible.
Edit:
The query should work by adjusting the second query to:
'"location":"SELECT location FROM page WHERE id IN (SELECT current_location.id FROM #friends)"';
When working with an array type in the FB API you can access the value in any query by doing array.value
Also, if all you need is the page's id, you'll probably get better performance by doing current_location.id in your first query
You're going to want to use a fql.multiquery, there is a similair answer here that can be tailored to your needs: Facebook Graph API get all users and their status
The FQL documentation on multiquery is located here: http://developers.facebook.com/docs/reference/fql/
Also are you trying to get the lat/long of a checkin object? If so you need to take a look at this resource: http://developers.facebook.com/docs/reference/fql/checkin/
Edit:
I'm not sure that what you are trying to do is possible. I just looked through the api and tested some queries, the only way you could get the lat/long from a friend seems to be based on a checkin, from there you can get details on the page if necessary... Even looking at your second query, current_location_id is not referenced anywhere in the page table, and selecting location from the page table is going to give you location info on the place that it pertains to. Maybe you could elaborate more on what you're trying to accomplish.
Here is a multiquery statement I wrote in PHP using FQL (it will select the coordinates of all your friends check ins, then from there get details on the place of the checkin pertaining to that friend):
try{
$multiQuery = "{
'query1':'SELECT coords, tagged_uids, page_id FROM checkin WHERE author_uid IN (SELECT uid2 FROM friend WHERE uid1 = me())',
'query2':'SELECT name, description FROM page WHERE page_id IN (SELECT page_id FROM #query1)'
}";
$param = array(
'method' => 'fql.multiquery',
'queries' => $multiQuery,
'callback' => ''
);
$queryresults = $facebook->api($param);
print_r($queryresults);
}
catch(Exception $o){
print_r($o);
}
fql?q=SELECT location,name FROM page WHERE page_id in (SELECT current_location.id from user where uid in(select uid2 from friend where uid1=me()))
this will do the magic for you
facebook.batch_fql({
'query1': "SELECT uid, name, current_location.id FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=me())",
'query2': "SELECT page_id, name, description, location FROM page WHERE page_id IN (SELECT current_location.id FROM #query1)"
})
will return two responses
one will be a list of friends UIDs, names and their current locations IDs
the other will be a list of unique location IDs with their name, description and geolocation info
EDIT: if you query for current_location using FQL, you'll get the full latlong in the first response and you don't even need the second one

Facebook current UID with FQL

How can i get my current UID with FQL.. i want to save traffic and requests with getting the the friends of a user with fql.multiquery
"query1": GET NEED MY USERID
"query2":"SELECT uid, first_name, last_name FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=MYUSERID) order by first_name"
should return my friends! getting users with friends.get and then users.getInfo needs 2 requests and is not optimal...
Thank you
Your first query should be entirely unnecessary, as Facebook always provides the uid to an application via the fb_sig_user parameter (POST or GET). I'm not sure what kind of FQL query you could use to get a uid based on some other form of information. There's no sessions table or anything available to FQL (available tables are listed here).
For multi-queries however, I'm fairly certain that this page describes exactly what you need to do, but I'll throw some code together in the way that I would do it.
If it was a PHP app, and I wanted to do this server side, I would create a couple of JSON-formatted strings and send them as a query as follows:
$fql= '{
"userinfo":"SELECT uid FROM user_standard_info WHERE first_name=\'zombat\'",
"friends":"SELECT uid2 FROM friend WHERE uid1 IN (SELECT uid FROM #userinfo)}"
}';
$res = $facebook->api_client->fql_multiquery($fql);
print_r($res['userinfo']);
print_r($res['friends']);
This should produce a dump of your query results.
Assuming you have your current uid available from the fb_sig_user parameter (which you should), then your problem should be solved by something like this:
$fql= '{
"friends":"SELECT uid2 FROM friend WHERE uid1='.$your_uid.',"
"friendinfo":"SELECT * FROM standard_user_info WHERE uid IN (SELECT uid2 FROM #friends)"
}';
$res = $facebook->api_client->fql_multiquery($fql);
Note: You may want to have a careful look at some of the queries you're trying to do. Many tables, such as friend, can only be used if you're dealing with a logged-in user.
The function me() returns your user id in FQL.
To get the list of your friends, you can use:
SELECT uid, first_name, last_name
FROM user
WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())
You can find the documentation of this here: http://developers.facebook.com/docs/reference/fql/