I am developing a Facebook app and I need to fetch a lot of data from users. I am concerned about limits Facebook has. I can't send queries one by one, I would soon get error 4 "API Too Many Calls". So I am wondering what is a better approach, if grouping queries into multiquery or sending them in batch. Btw I will have to use multiqueries in batch request. So here is an example :)
Suppose that USER_A and USER_B are friends so I can access them both with 1 access token.
multiquery for USER_A:
"Q0":"SELECT post_id FROM stream WHERE source_id=<USER_A>",
"Q1":"SELECT id FROM comment WHERE post_id IN (SELECT post_id FROM #Q0)"
multiquery for USER_B
"Q2":"SELECT post_id FROM stream WHERE source_id=<USER_B>",
"Q3":"SELECT id FROM comment WHERE post_id IN (SELECT post_id FROM #Q2)"
So what is better approach, grouping these 2 multiqueries into 1 bigger multiquery or putting them into a batch request? In which case will Facebook count it as 1 API call? Facebook has some limit like 100M API calls per day. So if I send my 2 multiqueries in batch / bigger multiquery, will facebook count it as 1 API call?
EDIT: by batch request i mean this https://developers.facebook.com/docs/reference/api/batch/
I am not asking if I should use FQL or Graph API, I want to know if there is a difference between sending multiqueries in batch and sending them as 1 bigger multiquery..
If you can do everything with one access_token, in my experience, a large FQL multiquery returns the results more quickly than a batch request.
Also, looking at your example query, you could cut the number of queries down by combining these:
'User_A_Comments':
"SELECT id, post_id FROM comment WHERE post_id IN (SELECT post_id FROM
(SELECT post_id FROM stream WHERE source_id=<USER_A>) )"
The advantage that a batch request would give you is you could use multiple access tokens to get this information in one shot.
Related
I'm trying to fetch most recent posts of several pages using facebook FQL. Here is my query:
# https://developers.facebook.com/tools/explorer/145634995501895/?fql=SELECT%20message%20FROM%20stream%20WHERE%20source_id%20in%20(755751084454823%2C%20616632928446954)
SELECT message FROM stream WHERE source_id in (755751084454823, 616632928446954)
And it returns 0 results. On the other hand, sending exact same query but for a single page ID returns posts for that given query properly. So, those two queries works great:
# https://developers.facebook.com/tools/explorer/145634995501895/?fql=SELECT%20message%20FROM%20stream%20WHERE%20source_id%20in%20(616632928446954)
SELECT message FROM stream WHERE source_id in (616632928446954)
# https://developers.facebook.com/tools/explorer/145634995501895/?fql=SELECT%20message%20FROM%20stream%20WHERE%20source_id%20in%20(755751084454823)
SELECT message FROM stream WHERE source_id in (755751084454823)
I can't quite understand why it behaves like that. Any help much appreciated.
To get posts from multiple Pages, use separate calls to the feed endpoint with the Graph API. Example code for this can be found in the docs:
https://developers.facebook.com/docs/graph-api/reference/v2.6/page/feed
You can use Batch Request to make those calls faster, it will take as long as the slowest API call in the batch: https://developers.facebook.com/docs/graph-api/making-multiple-requests
FQL is deprecated and will stop working in August 2016. You can´t use it in new Apps anymore.
I know there are many posts on this topic, but I've been really pouring over this one lately and wanted to hear the conclusion of some others who have been doing the same. I started just using the Graph API using the Graph style of getting data...
$facebook->api('/me/posts');
The problem is, it seemed pretty sluggish, but it was reliable. When I set a limit of 100 posts per query, I received those 100 posts, and was even given a friendly little paging link to get the next set.
But like I said, the query was sluggish, so I tried out FQL, which seemed to process things much faster, and gave me a far stronger bit of control over the data I was requesting, and gave me some real freedoms in what data I wanted specifically. But FQL sucks on reliability. I could craft a query asking for 1000 results and only get 93 results. I read that facebook grabs the 1000 then takes out the ones I shouldn't be seeing for various reasons, and gives me the left overs, which makes any sort of stepping difficult, and the results, while a little faster, pretty inaccurate.
$query = 'SELECT post_id, actor_id, comment_info, created_time, description_tags, like_info, message, message_tags, place, share_count, source_id, tagged_ids, type, with_location, with_tags FROM stream WHERE (type=46 OR type=56 OR type=80 OR type=128 OR type=247 OR type=285) AND source_id = me() AND (actor_id = me() OR actor_id IN(SELECT uid1 FROM friend WHERE uid2=me()) ) AND created_time > '.strtotime("-1 year") .' LIMIT 50000
That massive query only did get me 95 posts, and managed to get almost six months back in time.
I'll admit I'm still picking up the very finer in's and outs of FQL, but SQL is something I'm well versed in.
So my question is, should I stick with the tediousness of FQL and keep fine tuning those requests, or should I just come to terms with the lack of control and slightly slower speeds of using the friendly facebook Graph?
I'm writing a web app that allows users to search data from their Facebook profile as well as that of their friends (who've also authorized the app). Initially I was looping through the friends and making separate API calls to get the friend data, but that was SUPER slow. So I switched to building an array of API requests and submitting one batch request.
The batch request is supposed to be the solution to my problem, but it's STILL agonizingly slow. My page loads are around 15 seconds and I cannot figure out why. The documentation claims that each request in the batch is processed in parallel, but it sure doesn't seem that way. Is it relevant that each of my batched requests takes an unique access token? The documentation doesn't indicate that this is a problem, but the documentation doesn't say a lot of things...
Here's an example of one of my batch queries:
[
{"method":"GET",
"relative_url":"\/#####\/friends?fields=name,first_name,last_name,id,work,education&access_token=#####"},
{"method":"GET",
"relative_url":"\/#####\/friends?fields=name,first_name,last_name,id,work,education&access_token=#####"},
{"method":"GET",
"relative_url":"\/#####\/friends?fields=name,first_name,last_name,id,work,education&access_token=#####"}
]
Just to give you some context, the app makes one API request to get the user's friends. Then it loops through those results and builds a batch request for each friend (that has authorized the app) and sends a second API batch request using the PHP SDK ($json_batch is a batch request like the one shown above):
$rawdata = $facebook->api('?batch='.$json_batch, 'POST');
(exemplified above). The cumulative results are checked for matches against the user's search query and echoed back to the user. Any ideas why this should take 20 seconds to happen??
UPDATE:
I added some code to track the time at various times during the execution of the script...
The entire class runs in 11-13 seconds. The first FB api call (to the graph) takes 0.6 seconds. The second batch call is 10-11 seconds! But WHY?
I'm not sure why the batch requests take that long, but to answer the access token per request, no it's necessarily to do so, unless you put different tokens, if you are using the same token then you can simply (as in the documentation):
curl \
-F 'access_token=…' \
-F 'batch=[ \
{"method": "GET", "relative_url": "me"}, \
{"method": "GET", "relative_url": "me/friends?limit=50"} \
]'\
https://graph.facebook.com
You can however get the same exact information using a single FQL query:
SELECT uid, username, name, first_name, middle_name, last_name, work, education
FROM user
WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())
This also removes the need to query separably for the friends of the logged in user first, but if you do have a list of friend ids you can put those in the IN clause instead of the inner query.
Maybe this fql solution will return faster.
Is it possible to query more than one insights metric in one API call?
For example to get the daily added likes for a page I executed the following call:
https://graph.facebook.com/_object_id_/insights/page_fan_adds_unique/day/
But I would like to have another metric, e.g., page_fan_removes_unique, included as well.
Query the insights object entirely is a possibility, but gives me too much data I don't need, thus decreases performance.
Since your question shows that you're using the Graph API, you may want to use the batch requests method: http://developers.facebook.com/docs/reference/api/batch/
You can group multiple Graph API queries together so you can just query the Insights data you need in one request.
Yes, you can run a multiple FQL query in one API call and then parse the various results.
multi query: http://developers.facebook.com/docs/reference/rest/fql.multiquery/
insights table: http://developers.facebook.com/docs/reference/fql/insights/
I'm trying to make a simple "only status updates, nothing else" app (since FB doesn't seem too interested in this sort of thing). However, my queries seem to omit some results:
"select status_id,uid,time,message from status where uid in (select uid2 from friend where uid1= $my_id) ORDER BY time DESC LIMIT 25", for instance, when I last tested it, only returned 21 results (and yes, I've got enough friends who have made enough updates that there are definitely more than 25 historical statuses).
This is with the PHP API, by way of Heroku, but I've had similar issues for a while, going back to before FBML was deprecated, with basically the same query.
I haven't tested enough to determine absolutely that this is the case, but it seems only to return one status per user (if Bob posted six updates, it only returns his newest status and ignores the previous ones).
Is this a known issue/bug? Any workarounds?
facebook trades accuracy for performance it has been always the case , so it usually returns a limited number of results per query
It's probably because a user has not given access for a friends apps to access their posts. Apparently making an FQL request, facebook does the initial request, including your limit param. Then it filters the list removing anything that you don't have permissions to see. Hence receiving less than 25 results.