Need current syntax for writing an FQL query in PHP - facebook-fql

Because using the Graph to get comment information craps out at a certain volume (even with pagination) I need an FQL solution. Unfortunately, all the documentation and blogs I've found contain deprecated samples making it impossible for me to figure out the correct syntax.
I need to understand how to get this:
SELECT object_id, post_id, fromid, time, text, username
FROM comment
WHERE object_id
IN (SELECT comments_fbid FROM link_stat WHERE url='*** EVENT HREF ***')
into a PHP variable so I can access the data from a decoded JSON object like so:
$fql_obj = json_decode($fql_query, true);

I figured this out. For anyone else confused by it you append it to a graph lookup and replace spaces with +s:
$fql_query = "https://graph.facebook.com/fql?q=SELECT+object_id,+post_id,+fromid,+time,+text,+username+FROM+comment+WHERE+object_id+IN+SELECT+comments_fbid+FROM+link_stat+WHERE+url='***EVENTHREF***')";
$fql_obj = json_decode($fql_query, true);

Related

FQL: Get comment data WHERE object_id={a known object_id} works, but WHERE id={a known comment XID} does not?

I'm using FQL to get comment data from webpage with a Facebook comment box. I use the graph to get the object_id, then use FQL to get all comments with that object_id. However, if I want to pull data from a specific comment using its id (sometimes referred to as an XID) the FQL returns 0 results.
According to https://developers.facebook.com/docs/reference/fql/comment/ the id should be indexed, so this query should work:
SELECT+id,+text,+time,+fromid,+is_private+FROM+comment+WHERE+id={some known XID}
But it doesn't. BTW, THIS query does work:
SELECT+id,+text,+time,+fromid,+is_private+FROM+comment+WHERE+object_id={some known object_id}
It is irritating how I can't pull query for comments using time or fromid or anything other than object_id, id, post_id, and parent_id... but in practice only using the object_id returns anything.
has anyone else run into this problem? Is my XID query wrong somehow, or is the id only indexed on certain kinds of comments, and not webpage comments for no inexplicable reason?
This one I managed to solve with trial and error. I had to put the id in single quotes. It's odd, I could query the object_id without putting it in quotes, but not the id. This is sort of what the working FQL looked like, except with a valid access token:
SELECT+id,+text,+time,+fromid,+is_private+FROM+comment+WHERE+id='#########_####'
as opposed to:
SELECT+id,+text,+time,+fromid,+is_private+FROM+comment+WHERE+id=#########_####
which failed to return any results.
Thanks for being consistent, Facebook :(

Why FQL query for Post content returns empty data[]?

I'm trying to run this FQL and get the list of comments on a specific post:
select post_fbid, fromid, object_id, text, time FROM comment WHERE object_id = '518460828241435_546620402092144'
Why it is returning this:
{
"data": [
]
}
PS: I'm testing it on graph api explorer tool.
Thanks for the help! Really appreciate the help!
Use post_id instead of object_id. That is how I am using it.
Or else you are using an incorrect id that you pass as object_id :)

Any way to use elements of FQL attachment media array in multiquery?

I need to get the list of comments for each item in a user's news feed including comments for any media in the feed items.
I'm trying to avoid multiple FQL query roundtrips, so I'd like to use multiquery.
I can get the comments for each post_id in the feed:
{
posts: 'SELECT post_id, attachment FROM stream WHERE filter_key = "nf"',
post_comments: 'SELECT post_id, fromid, text FROM comment WHERE post_id IN (SELECT post_id FROM #posts)'
}
However, there doesn't seem to be a way to reference content in the media elements so I can request the comments for media fbids, e.g.:
photo_comments: 'SELECT object_id, fromid, text FROM comment WHERE object_id in (SELECT attachment.media.fbid FROM #posts)'
This question (How to query FQL Stream by Attachment.Media.Type?) is very similar, but the answer was a bit uncertain & wasn't accepted.
Any suggestions or definitive answer?
Figures -- once I posted the question, I came across an answer to another question (http://facebook.stackoverflow.com/questions/8003581/get-photos-from-stream) that gave me what I needed:
photo_comments: 'SELECT object_id, fromid, text from comment where object_id in (SELECT attachment.media.photo.fbid from #posts)'
The example in my answer was a generic description, but turned out to be close to the actual technique (with the photo object missing in the path).
Hope this helps somebody else doing a similarly narrow search.

FQL search public posts

I'm trying to do a search using FQL.
Using the Graph API, it works but there are more options using FQL.
Using something like
https://graph.facebook.com/search?q=myquery&access_token=mytoken
it work fine.
I'm looking for the equivalent in FQL.
What query I must write in here
https://api.facebook.com/method/fql.query?query=SELECT%20post_id%2C%20actor_id%2C%20target_id%2C%20message%20FROM%20stream&access_token=mytoken
The query above give me that
Parser error: unexpected end of query
I want to search in all public posts.
I've been looking everywhere but I did not found any solution.
Thanks.
For searching public posts for a string, you need to use the Graph API, and then filter those posts in your script. I don't think searching all public posts is possible in FQL. While FQL is more powerful, it is also more limited.
You are getting an error because you don't have a "WHERE" clause in your query. This is required in FQL.
The limit comes in because you must use at least one indexed column in your WHERE query. For the stream table you must specify either a post via post_id, a user via source_id or filter_key, or a live stream via xid. The indexed fields are marked with a * on the documentation site.
For instance, [this FQL][1]
SELECT post_id,actor_id,target_id,message FROM stream WHERE filter_key = 'others'
AND strpos(message, 'the') >= 0
will get you all posts that show on your access token owner's wall that have not been posted by the owner, with the string 'the' in them. That is the best you can get. If the post isn't visible on their wall, then you won't get the post.
If you try to leave out an indexed field, FQL will throw a 'Your statement is not indexable' error.
[1]: SELECT post_id,actor_id,target_id,message FROM stream WHERE filter_key = 'others' and strpos(message,'the') >=0

A simple question about facebook comments plugin

I'm struggling with a very simple problem. The facebook documentation, as always, didn't give me enough explanation.
I attached a facebook comments plugin to my site. And using a callback of "comment.create" event, I can get information of a just-create comment.
FB.Event.subscribe('comment.create', function(response) {
alert(JSON.stringify(response));
});
The json reponse looks like:
{"href":"http://siteaddress.com/page.htm", "commentID":"111122223333" }
What I like to do now is to retrieve the data of the single comment with the commentID. Although I expected that the following way should work:
https://graph.facebook.com/111122223333
it just gave me "False". I can retrieve all comments attached to that page using:
https://graph.facebook.com/comments?ids=http://siteaddress.com/page.htm
But, what is the right way to retrieve the single comment data just created using the commentID?
I was also facing the same problem... so what i did was , I queried the last posted comment or reply from the fb comments table using fql. Here I sort the comments by time in descending order and pick the top one. though one may think that if two comments are posted at same time, it may cause ambiguity, But in my case i tried and tested it invoving more than 2 users but i always got the expected result.
FB.Event.subscribe('comment.create', function(response) {
FB.api({
method: 'fql.query',
query: "select post_fbid, fromid, object_id, text, time from comment where object_id in (select comments_fbid from link_stat where url ='URL_OF_THE_COMMENT_BOX') or object_id in (select post_fbid from comment where object_id in (select comments_fbid from link_stat where url ='URL_OF_THE_COMMENT_BOX')) order by time desc limit 1"
},
function(response) {
var feed = response[0];
alert(feed.text)
}
);
});
Hi if you are having comment id then why you dont use FQL and query to Comment Table to get all comment related data?
I am having this same problem. What seems to be happening is that the commentID and parentCommentID are actually just returning the unique ID of that page, and not a unique ID for the comment itself.
The unique ID for an individual comment is the unique ID of the page (that is, the value currently being returned as "commentID") with an underscore followed by another number (8 digits long in the tests I've done). You can look this up directly from the graph the response provided it.
I have logged a bug with Facebook to hopefully get this fixed! Bug at address below:
http://bugs.developers.facebook.net/show_bug.cgi?id=16535
Hm. I can retreive comment data by id (id format like this: 1234567890123456_12345678). Ids i retreive from url like this:
https://graph.facebook.com/comments?ids={$url}
I combined a couple approaches (including from Charsee).
// this query takes a "commentID" and "href". The commentID is returned on comment.create "response" object
// this code requires an escape function "addslashes(str)" to handle single quotes.
var query = "SELECT text, fromid FROM comment WHERE post_fbid='"+addslashes(commentID)+"' AND ( object_id in (select comments_fbid from link_stat where url ='"+addslashes(href)+"') or object_id in (select post_fbid from comment where object_id in (select comments_fbid from link_stat where url ='"+addslashes(href)+"')))";