How to find out the total number of groups created by a user - facebook-fql

i'm playing with these code and it fails:
$fql = "SELECT name FROM group WHERE gid IN (SELECT name FROM group WHERE version=1) AND creator=me()";
$ret_obj = $facebook->api(array(
'method' => 'fql.query',
'query' => $fql,
));
echo count($ret_obj["name"]);
does anyone knows how to count() the no. of groups a person has created. Thanks
i'm using PHP-SDK.

Seems like group table has only 1 field gid as indexable which makes it impossible to filter user groups in FQL. Possible bug.
What you can do is make a graph API call to:
https://graph.facebook.com/me/groups?fields=name,owner
and then filter results by owner.id and count them.
hope this helps

Related

Facebook: Find out when user liked an URL

I can get pair of user_id - url from url_like table.
The only two columns are, as mentioned, user_id, url.
I tried to get get date of 'liking' the URL by many ways, by connecting multiple tables, but no luck.
One of many examples might be
SELECT url, id, type, site FROM object_url WHERE url IN (
SELECT url FROM url_like WHERE user_id = {$target}
) LIMIT 1000",
Is there any way how to get date/timestamp of when user liked particular URL?
As said I even tried impossible...
Facebook doesn't offer time when use liked an URL. And I don't think they will be giving it in future. It comes under Facebook privacy policy.
You can trigger event when user liked url and insert time in your database by ajax. If user unlike and like it again then update same records. I have previously worked on subscribing like event by Javascript. SDK
You can find how to subscribe to an Like in the the documentation here:
FB.Event.subscribe('edge.create',
function(response) {
alert('You liked the URL: ' + response);
//Insert your user id with time when user liked by AJAX
}
);
There is no API which will return the time a Like was created - the associated /<user id>/feed story for a like will tell you this information, but not all Likes have feed stories associated with them, and finding the story will be extremely time consuming.
You could track when the Like button is clicked if you're using the Like Button social plugin, but you'd also need the user's permission to see their user ID, etc, in order to log this usefully.
I found this solution that solves my problem
I rely on FQL
public static $since = '2012/01/01';
...
...
// $target1 stands for "me", it has value of my FB ID
// So I am selecting all links I liked in past year
$object_id_raw = static::$facebook->api(array(
'method' => 'fql.query',
'query' => "SELECT link_id FROM link WHERE owner = {$target1} AND created_time > ".strtotime(static::$since)
));
// Just extracting IDs for later usage
$object_id = array();
foreach($object_id_raw as $oid):
array_push($object_id, $oid['link_id']);
endforeach;
// - friendId stands for FB ID of my friends
// This query is selecting from links I liked,that means we have them liked both...
// This API call returns object ID's (ID's of links). Do whatever you want with them...
$result = static::$facebook->api(array(
'method' => 'fql.query',
'query' => "SELECT object_id FROM like WHERE object_id IN ('".implode("','", $object_id)."') AND user_id = {$friendId}"
));

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

Changing video thumbnail_link in Facebook

Can I update or change the thumbnail_link for a video in Facebook?
I access the video attributes with this query:
$facebook->api(array('method' => 'fql.query', 'query' => "SELECT vid, thumbnail_link FROM video WHERE owner='$user_id' AND vid=22170513xxxxx" ));
Now, if I want to change the thumbnail image for the video, how can I do it?
I tried the FQL:
'query' => "UPDATE video SET thumbnail_link ='xyz' WHERE owner='$user_id' AND vid=22170521xxxx"
But it doesn't work, so what can be the process?
FQL is only for querying data exposed by the Graph API, so you can only fetch data. You can read the documentation in Facebook Query Language (FQL).
Queries are of the form:
SELECT [fields] FROM [table] WHERE [conditions].

Search for people by name in FQL

I'm trying to develop a search for people by name feature using the Facebook API.
I'm trying this FQL query:
SELECT uid, username, name, pic_square FROM user WHERE strpos(name, 'Alfredo Artiles') >= 0
But I get an "Your statement is not indexable. The WHERE clause must contain an indexable column." error.
I also tried to add a "and uid > 0" condition, but that didn't work too.
Any idea?
SELECT uid, username, name, pic_square FROM user WHERE contains('Alfredo Artiles')
I have no idea why it works, as it violates the "at least and indexable field in WHERE", but it does.
EDIT: this is where I read it: link
later edit
Ok, sorry for the mistake regarding strpos, I didn't remeber it existence last time i checked the fql docs. The thing about it is that it can be used just in certain cases. You need to have a primar indexable column in your where clause and a second condition with strpos (at least this is how I succeded using it). For example:
SELECT actor_id, message FROM stream WHERE source_id = me() AND strpos(message, 'stuff') > 0 limit 50
I hope this clarifies a little bit the confusion with this function.
U need to specify the uid in where clause fetch details in facebook user table. we don't get the the all user details.here get the details of currently logged in user
"SELECT name FROM user WHERE uid IN(SELECT uid2 FROM
friend WHERE uid1 = me()) AND strpos(name,'Alfredo Artiles') >=0"
YES, YOU CAN!!!
Try to search in profile table, instead of user table. In profile table, the name field is indexable, so your statement would have to change only a bit:
SELECT id, username, name, pic_square FROM profile WHERE contains('alfredo artiles') and type='user'
Contains is not case-sensitive while strpos is. You could still use lower and strpos, but you would also have the problem of special characters. Contains is the solution.
You can find all fields of profile table in facebook doc:
Hope it helps!

Facebook Comments Count

I am trying to write a Facebook query that will return all comments posted by a user to his friends,
however I can't seem to find the correct schema. Its as if there are no 'indexable' fields to build this.
Any suggestions please?
With thanks,
Wineshtain
The indirect path for stream comments would be something like
select * from comments where fromid = <my_id> and object_id in (
select post_id from stream where sourceid in (
select uid1 from friend where uid2 = <my_id> ) )
for photos, substitute the middle query with
SELECT pid FROM photo WHERE aid IN ( SELECT aid FROM album WHERE owner IN ( ...
Unfortunately the security settings may restrict the querying your friends wall posts and photos.
I don't believe you can accomplish this in a direct manner as you describe. FQL tables are generally only indexed on a limited criteria (for performance reasons I'm sure). In the case of the Comments FQL Table, you can only select comments via a post ID or an xid.
Unfortunately, this means that you have to know the objects that a user has commented on before you can get the comments for it. You would have to have previously selected all the posts, photos, etc. that you wished to get the comments for before you could retrieve them.