This is my code but doesn't work:
FB.api(
{
method: 'fql.query',
query: 'SELECT uid, first_name, last_name FROM user WHERE uid=100002306311953'
},
function(data) {
alert(data.uid);
}
);
});
I get "undefined" in the alert.
Why ?
Welcome to SO!
You can use the FB.api function by passing the fql.query method and the query,
A simple query that returns the current user, name:
FB.api({
method: 'fql.query',
query: 'SELECT uid, name FROM user WHERE uid = me()'
}, function(data) {
console.log(data);
var res = data[0].name;
alert(res);
}
);
FB Docs: https://developers.facebook.com/docs/reference/fql/
Related
I'm working on a Facebook app and I need to know if the user likes a specific page or not.
I tried using:
$isFan = $facebook->api(array(
"method" => "fql.query",
"query" => "SELECT uid FROM page_fan WHERE page_id = 'SOMEPAGEID'
AND uid = me()"
));
It returns NULL.
I even tried the javascript SDK:
FB.api('/me/likes/SOMEPAGEID', function(response) {
console.log(response.data);
});
It returns nothing.
I'm asking for the following permissions:
$scope = "email,user_likes";
Am I missing something?
Edit: SELECT * was bad copy-paste... it's SELECT uid
Your permissions look correct but I don't think that FQL queries actually support SELECT *, you have to manually list the fields
So try changing your FQL query to
select page_id from page_fan WHERE uid = me() AND page_id = PAGE_ID
Or it might be you haven't encoded your query properly. This runs for me in the graph explorer https://developers.facebook.com/tools/explorer?method=GET&path=fql%3Fq%3Dselect%2Bpage_id%2Bfrom%2Bpage_fan%2BWHERE%2Buid%2B%253D%2Bme%2528%2529
fql?q=select+page_id+from+page_fan+WHERE+uid+%3D+me%28%29
so try using
$isFan = $facebook->api('/fql?q='.urlencode('select page_id from page_fan WHERE uid = me() AND page_id = PAGE_ID'));
I'm trying to display Facebook comment counts in <div id="comments">
It has to be via Facebook Query Language (FQL). This post is almost exactly what I need:
Facebook Graph Api url comments and shares count doesn't work anymore
But how do I display the comment_count (from the query) into a div? i.e. how do I process that data? So far, I have:
$(function(){
$.ajax({
url: 'https://graph.facebook.com/fql?q=SELECT%20comment_count%20FROM%20link_stat%20WHERE%20url=%27e',
dataType: 'jsonp',
success: function(data) {
if(data.comment_count)
{
$('body').find('#comments').html('Comments ('+jsonp.data.comment_count+')');
}else{
$('body').find('#comments').html('Comments (0)');
}
}
});
});
I did it like this to update my div with the likes count like this
$fql = "SELECT url, normalized_url, share_count, like_count, comment_count, ";
$fql .= "total_count, commentsbox_count, comments_fbid, click_count FROM ";
$fql .= "link_stat WHERE url = '".$url."'";
$j.ajax({
url: 'https://api.facebook.com/method/fql.query?format=json&query=<?php echo urlencode($fql);?>',
dataType: 'jsonp',
success: function(data)
{
$j(".comment_count").html(data.comment_count);
}
});
works for me like a charm.
For my part,
I used php code to get the comment count via fql. First, you need to download the facebook php sdk and load it at the top of your page:
require_once("src/facebook.php");
$config = array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_SECRET_KEY',
);
$facebook = new Facebook($config);
Then, the fql query:
$url = 'http://www.yoururl.com/;
$fquery = 'SELECT comment_count, share_count, like_count FROM link_stat WHERE url = "'.$url.'"';
$fparam = array( 'method' => 'fql.query', 'query' => $fquery );
$fql = $facebook->api($fparam);
$cmcount = $fql[0]['comment_count'];
So, $cmcount is now your comment counts, put it directly in your html code:
<div id="comments">
<?php echo $cmcount; ?>
</div>
I've setup the following permissions, both on my side and app's permissions' page:
(notice the "read_stream")
<fb:login-button autologoutlink="true" perms="read_stream, user_about_me, user_likes, user_location, user_birthday, user_education_history, user_work_history, friends_about_me, friends_likes, friends_location, friends_birthday, friends_education_history, friends_work_history"></fb:login-button>
Requesting for the user's profile works great:
FB.api('/me/friends', {
fields: "id,username,name,first_name,last_name,location,link,education,work,statuses.limit(1).fields(message)"
}, function(res){
console.log('FRIENDS', res.data);
});
However, getting the same for the user's friends doesn't work.
It gives a "500 (Internal Server Error)", with facebook object returning "unknown error".
FB.api('/me/friends', {
fields: "id,username,name,first_name,last_name,location,link,education,work,statuses.limit(1).fields(message)"
}, function(res){
console.log('FRIENDS', res.data);
});
I've traced the issue to the field I request. The part statuses.limit(1).fields(message) seems to be the source of the error, when removed the /me/friends returns data.
But I don't understand the reason for the error.
It works for /me.
According to FB docs read_steam should be for both user's statuses and user's friends' statuses.
With the above fields the API Explorer gets me the friends statuses without any issues.
Am I doing something wrong or is it a FB Graph API bug?
Thank you.
Here try this:
$user = $facebook->getUser();
if ($user){
try {
$queries = array(
array('method' => 'GET', 'relative_url' => '/'.$user),
array('method' => 'GET', 'relative_url' => '/'.$user.'/friends', 'name' => 'get-friends'),
array('method' => 'GET','relative_url' => '?ids={result=get-friends:$.data.*.id}'),
);
// POST your queries to the batch endpoint on the graph.
try{
$batchResponse = $facebook->api('?batch='.json_encode($queries), 'POST');
}catch(Exception $o){
error_log($o);
}
}
catch (FacebookApiException $e) {
error_log($e); //Checks if user is logged out and generate an exception if access token is invalid
}
}
else{
$params = array(
'scope' => 'friends_birthday, friends_education_history, read_stream, read_friendlists, user_birthday, //Requesting User Permissions through Facebook App
'redirect_uri' => 'specify yours here' //User is redirected after Login
);
}
$user_details = json_decode($batchResponse[0]['body'], true);// User Own Info
$user_friends = json_decode($batchResponse[2]['body'], true);// User's Firend Info
You'll get an array object.
Or You could use FQL, here :
$friends = $facebook->api(array(
'method' => 'fql.query',
'query' => 'SELECT name, pic_big
FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())'
));
https://developers.facebook.com/docs/reference/fql/friend/
I'm working on something similar, here: http://agbcorplegal.com/profile_filter/
Is it possible to get messages from specific user?
If I would like to get all messages where recipients are myself and x
I didn't find a way to create that kind of query, so is it possible?
var fbid = your_fbuid_here;
FB.api({
method: 'fql.query',
query: 'SELECT thread_id, author_id, created_time FROM message WHERE thread_id IN (SELECT thread_id FROM thread WHERE folder_id = 0) AND author_id = ' + fbid + ' ORDER BY created_time ASC LIMIT 1'
}, function ( threadresponse ) {
FB.api({
method: 'fql.query',
query: 'SELECT thread_id, body, author_id, created_time FROM message WHERE thread_id = ' + threadresponse[0].thread_id + ' ORDER BY created_time ASC'
}, function ( inboxresponse ) {
//do stuff here with results
});
});
or you can do this
var fbid =the _freind_fb_uid_here;
FB.api({
method: 'fql.query',
query: 'SELECT thread_id, body, author_id, created_time FROM message WHERE thread_id IN (SELECT thread_id FROM thread WHERE folder_id = 0) AND author_id = ' + fbid + ' ORDER BY created_time DESC'
}, function ( threadresponse ) {
//do stuff here with results
});
facebookfacebook-fqlfacebook-apifacebook-graph-apifacebook-fql-query
Now FQL don't have field author_id, only originator and snippet_author. But this fields contain wrong data. For example, userA send me (userB) message: userA create thread. Anyway I see, that originator have me (userB).
Best way use recipients:
SELECT recipients,snippet,object_id,updated_time,unread,unseen,thread_id FROM thread WHERE folder_id=0 AND recipients IN (userA_fid, userB_fid)
But it's also don't work and return empty data...
Is there a way to use Facebook's FQL query using the current graph API? Or we still have to use the legacy API?
Example of Graph API and JavaScript:
FB.api(
{
method: 'fql.query',
query: 'SELECT uid, first_name, last_name FROM user WHERE uid = ' + someUid
},
function(data) {
// do something with the response
}
);
http://developers.facebook.com/docs/reference/javascript/
http://developers.facebook.com/docs/reference/fql/
Example Of Rest API:
$data = $facebook->api( array( 'method' => 'fql.query', 'query' => 'SELECT shit FROM table...' ) );