How do I know the current user liked corresponding link? - facebook

I make a page tab application. The application displays some items, each of which has a like button. Like buttons attached to the corresponding url item. I need to display to users who liked, some text in those elements that have collected 10+ likes.
So, I write this code:
// Get a link statistics
$link_stat = $fb->api(
array(
'method' => 'fql.query',
'query' => 'SELECT like_count FROM link_stat WHERE url="http://myurl.com/item1.html"'
)
);
if ((int)$link_stat[0]['like_count'] > 10)
{
//Get like by current user_id and link object_id
$like = $fb->api(
array(
'method' => 'fql.query',
'query' => 'SELECT user_id, object_id FROM like WHERE user_id=me() AND object_id IN (SELECT id FROM object_url WHERE url="http://myurl.com/item1.html")'
)
);
var_dump($like);
}
I run this code upon request permission *user_likes* and *read_stream*, but it outputs array(0) { }.
A question is, how do I know the current user liked corresponding link?

Oh, sh… I didn't notice that there is a table url_like (or recently emerged?).
I made this FQL query, and it returned the required response:
SELECT user_id FROM url_like WHERE user_id = me() AND url = "http://myurl.com/item1.html"
Cpilko, thank you very much! =)

You could try rewriting your query to go at it the other way. See if your url exists in a user's likes.
This works for me:
SELECT url, id, type, site FROM object_url WHERE id IN (SELECT object_id FROM like
WHERE user_id=me()) AND strpos(url, "myurl.com/item1") >= 0

Related

facebook batch/fql friendlists a friend is member of

I'm creating a javascript facebook app to have an overview of my friendlists. Actually I can retrieve my (logged in user = me) friendlists, when I click a friendlist I see all my friends in that list.
When I want now is, when I click a list, I want to see the friends belonging to that list, and a comma separated lists my friend is member of.
For example in the list "Best" I have 3 friends: Athos, Portos, Aramis. But they are also in other lists. So in my app I'd like to get something like this:
Athos: Best, Family, Close Friends, VIP
Portos: Best, VIP
Aramis: Best, Close Friends, Party Animals
I tried with the FQL multiquery, but it seems impossible to get what I want.
So I think I should use a Batch request, but since I don't have a lot of experience using batches here I am asking your help.
So here the FQL query I use to get friends belonging to the list. I need this, because I want to manage friends beginning from a list:
var listID = 123;//just enter a listID
var friendsInList = 'SELECT uid,username,profile_url,first_name,last_name,pic_square FROM user WHERE uid IN (SELECT uid FROM friendlist_member WHERE flid =' + listID +')';
Now I don't know how to proceed in a clean and efficient way. I have a working but inefficient solution:
//[...]FB.api call to get a response object with all my friends using the friendsInList query above
for (var i = 0; i < response.length; i++) {
friendID = response[i].uid;
//call FB.api with the following query:
var friendListMemberships = 'SELECT uid, flid, name FROM friendlist_member WHERE flid IN (SELECT flid FROM friendlist WHERE owner=me()) AND uid IN (select uid FROM friendlist WHERE flid =' + friendID;
}
the above solution works but is highly inefficient as I send a FB.api request for each friend, very slow for big lists... thus I'd like a pro user to help me putting this in a batch (a batch can have maximum 50 requests, thus I'd like to send just 1-2 requests and get back all what I need).
I know how to write a batch, here I put the structure of my code, if it can help you completing it with the second query:
var listID = _listID;
var _queryFriendsInList = 'SELECT uid,username,profile_url,first_name,last_name,pic_square FROM user WHERE uid IN (SELECT uid FROM friendlist_member WHERE flid =' + listID + ')';
var queryFriendsInList = _queryFriendsInList.replace(" ", "+");
var _queryFriendListMemberships = 'I have no idea what kind of query I can write here to get the lists my friends from queryFriendsInList are member of';
var queryFriendListMemberships = _queryFriendListMemberships.replace(" ", "+");
var searchArgs = {
access_token: FB.getAuthResponse().access_token,
batch: []
};
searchArgs.batch.push({
'method': 'GET',
'name': 'friendsInList',
'relative_url': 'method/fql.query?query=' + queryFriendsInList,
'omit_response_on_success': false
});
//here is where I need help
searchArgs.batch.push({
'method': 'GET',
'name': 'friendlistMememberships',
'relative_url': 'method/fql.query?query=' + queryFriendListMemberships,
'omit_response_on_success': false
});
FB.api("/", "POST", searchArgs,
function (response) {
//console.log("log");
}
);
I hope the question and code samples are clear enough. Thank you for helping!
You can get everything you want in one API call with a multiquery. You'll need to assemble this together on the client side with a few loops, but it shouldn't be too hard. Here's the query:
{
"all_friendlists":
"SELECT flid, owner, name FROM friendlist WHERE owner=me()",
"fl_members":
"SELECT uid, flid FROM friendlist_member WHERE flid IN
(SELECT flid FROM #all_friendlists)",
"fl_member_details":
"SELECT uid, name, username FROM user WHERE uid IN (SELECT uid FROM #fl_members)"
}
You can present everything to your user from this one API call. The #all_friendlists query gives all your user's friendlists. Once your user has selected a friendlist, then loop through #fl_members to find all the members of this list, retrieving their details from #fl_member_details. Make another loop through #fl_members to find what other lists they belong to.
Alternately, you could just mash these three lists together into a single presentation page, and use a jQuery data filtering scheme like Isotope to allow your user to sort/filter/etc. on the fly.

fql multi query

FB.api(
{
method: 'fql.query',
query: 'select uid,name from user where uid in (select uid2 from friend where uid1 ='+me.id+') AND current_location='+me.location.name
},function(data) {
alert(data);
}
iam trying to get the friends based on the current user location.But this query is not working? Is there any possible way to write this as a single query?
I need to loop all the datas to check the current location else ,but it takes time for checking it.So iam looking for single query to get the datas? Could any one help me with it?
This is just an idea,
firstly your application requires user_location and friends_location as permissions.
Why don't you modify your query to something like this
FB.api(
{
method: 'fql.query',
query: 'select uid,name,current_location from user where uid in (select uid2 from friend where uid1 ='+me.id+')
},function(data) { alert(data);
}
The idea is to get current_location of user and user's friends and then do your filtering.
Hope this helps

Get user name of comment in comments table FQL

I am getting Facebook comments data for particular posts using FQL. FQL returns only fromid however i need fromname as well. I am using batch request for getting comments of 50 posts at same time.
My query is
SELECT object_id,post_id,fromid,time,text,id,reply_xid,post_fbid,is_private FROM comment WHERE post_id='".$postID."'
How can I do this?
you need to make an additional query, something like this:
$multiQuery_postInfo = array(
"posts" => "SELECT post_id, actor_id, message, message_tags, likes, comments, share_count, attachment, action_links, type FROM stream WHERE source_id=$pageID",
"users" => "SELECT uid, username, first_name, last_name, profile_url, pic FROM user WHERE uid IN (SELECT actor_id FROM %23posts)"
);

Error getting photos from a Facebook album using FQL and Javascript SDK

I use this function to get photos from a facebook album. Please give advice why it returns blank space.
function getPhotos(aid) {
sql = 'SELECT pid, src_big, src_small FROM photo WHERE aid='+aid+' ORDER BY modified desc';
FB.api(
{
method: 'fql.query',
query: sql
},
function(response) {
console.log(response);
}
);
}
While posting the error would help, you should wrap the aid in quotes since it's a string field (ref):
sql = 'SELECT pid, src_big, src_small FROM photo WHERE aid="'+aid+'" ORDER BY modified desc';
Also make sure you have the right permission user_photos.

Facebook FQL - All fanpage Events a User is Attending

I am trying to achieve the following via Facebook's Multiquery FQL, get a list of all Fan Page Events that i am 'Attending'.
I have the below logic, and have tried to implement something similar with the FQL statement below.
get list of all events created by / from a Fan Page (using specific id)
get a list of all my (the users) events attending
Match eid's from event_member FQL Table
If they match ids, and rsvp_status is equal to attending
display result.
FQL Multi
{"query1":"SELECT eid FROM event WHERE
eid IN (SELECT eid FROM event_member
WHERE uid
=$fanpageID)","query2":"SELECT rsvp_status FROM event_member WHERE
uid = $uidAND eid IN
(SELECT eid FROM #query1)"}
I know the statement is incorrect, but i am struggling on trying to do this. I have achieved this via Graph API using a very messy set of foreach loops in my PHP code.
Using a single multiquery FQL statement would be far more efficient.
Any suggestions would be great.
EDIT
Problem im having now, is ifaour's FQL statement works within the FQL Query console (Returns Results), however when i run the following, and empty array is returned.
When i dissect the statement to just ( SELECT eid FROM event WHERE creator =$fanPageID) the FQL Console highlights that eid in non indexable.
Here is my PHP
function usersEventsPoints($uid,
$facebook, $fanpageID){
$events = "SELECT eid FROM event WHERE creator=$fanpageID AND eid
IN (SELECT eid FROM event_member WHERE
uid=$uid AND rsvp_status =
\"attending\")";
$eventsAttendance = $facebook->api(array(
'method' => 'fql.query',
'query' =>$events, ));
$eventsAttendingcount = 0;
foreach
($eventsAttendance as $eventsAttendanceFetch) {
if($eventsAttendanceFetch['eid'] == true){
$eventsAttendingcount++;
}
}
return $eventsAttendingcount;
}
Blockquote
Any Ideas?
It's a very simple task:
SELECT eid FROM event WHERE creator = $fanpageID AND eid IN (SELECT eid FROM event_member WHERE uid = me() AND rsvp_status = "attending")
This will return all events' Ids that are created/owned by the page that has $fanpageID id and you are a member in (me()) and your status is attending.
EDIT:
You are looping the returned array just to get the count?! you can simply use:
$eventsAttendingcount = count($eventsAttendance);
Also make sure that the user you are matching, has authorized your application and granted you the correct permission!
I'm not sure I understand what the problem is - I ran your FQL statement and it returns a list of events and then your RSVP status?
On another note, why not use the events.get method. You pass in the UserId and it will display the event information for you. You can filter by RSVP status.
Just checked and the events.get method is in the process of being deprecated.
You should use the new Graph API which gives you a list of attendees for your event:
http://developers.facebook.com/docs/reference/api/event
You get the list of attendees by using
https://graph.facebook.com/EVENT_ID/attending
If you want to get the next 10 events of the facebook logged user where events are set a attending then you can build your Swift query like this:
let graphPath = "/me/events?limit=10&type=attending&since=now"
let params = ["fields": "id, name, category, start_time, end_time, timezone, updated_time, type, attending_count, owner, place"]
let request = GraphRequest(graphPath: graphPath, parameters: params, accessToken: accessToken, httpMethod: .GET, apiVersion: apiVersion)
request.start {}