How to use Facebook's FQL query using graph API? - facebook

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...' ) );

Related

Facebook API - comment count via FQL

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>

How to do a FQL query with JS SDK?

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/

fql.get method return empty Array in Facebook Application with facebook-php-sdk-v2.1.2-4

Recently I am trying to develop a facebook application that shows the STATUSES of loged in facebook user.
I am succeeded to retrieve information from user table using FQL. But right now I am using this following code to get data from status table.
****BEGIN :: CODE SECTION***********
$sql = "SELECT message,time FROM status WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = ".$myLoginId.")";
$param = array(
// 'method' => 'fql.query',
'method' => 'status.get',
'query' => $sql,
'callback' => ''
);
$fqlResult = $facebook->api($param);
print_r($fqlResult);
***END:: CODE SECTION********
But everytimes it returns Empty Array.
Right Now I am using Library of facebook-php-sdk-v2.1.2-4-g2343fca.tar which I collect from github.com.
can anybody please give me a solution.
Thank in Advance.
You code is working just fine, just a small note, you can use me() to get the current user id:
$fql = "SELECT uid,message,time FROM status WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me())";
$fqlResult = $this->facebook->api(array(
'method' => 'fql.query',
'query' => $fql
));
AND once again, the most important part is acquiring the correct permissions, which are user_status (if you want to get your statuses) AND friends_status (to get your friends' statuses).

how to use fql query

i am new at facebook application development i want to know how fql query works i used it in my application but i want to know where fql query results are displayed or how they can be displayed if you can't explain plz send me the link because the official documentation didn't helped me either.
Connect to FB using the PHP SDK. (available here)
ini_set('display_errors',1);
error_reporting(E_ALL);
require_once 'library/facebook/src/facebook.php';
Check if the user has logged in to FB
$facebook = new Facebook(array(
'appId' => 'YOUR API ID',
'secret' => 'YOUR APP SECRET',
'cookie' => true, // enable optional cookie support
));
FQL
try {
$me = $facebook->api('/me');
$currentPage=1;
$currentPost=1;
$currentDate="";
$list="";
$arrDate=array();
$fql = "YOUR FQL STATEMENT HERE";
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$fqlResult = $facebook->api($param);//Here you have your FQL result set
foreach($fqlResult as $row){
//Do something to the result
}
} catch (FacebookApiException $e) {
error_log($e);
}
You can use the Facebook Graph Explorer
https://developers.facebook.com/tools/explorer
And select under the Access token field, "FQL query".
Now you can try FQL queries and see the result of them.

Check if user is a fan of my Facebook app

How can I check thought the FB Javascript API if the current user is a fan of my app? I need to check this from within the page inserted into the FB's canvas IFRAME, throught the JS SDK...so that I can call a method of a local Flash, and do "something special" for that user.
In PHP, you can achieve this by using their PHP-SDK.
First you're gonna have to query their FQL database. Use the ff query (replace UID with the user's Id and PAGE_ID with your application's Id).
'SELECT uid, page_id FROM page_fan WHERE uid=UID AND page_id=PAGE_ID'
So your code might look like this:
$client = new Facebook(YOUR_PARAMS_HERE);
$result = $client->api(array(
'method' => 'fql.query',
'query' => 'SELECT uid, page_id, type FROM page_fan WHERE uid=1234 AND page_id=4321',
));
This returns an array similar to the ff:
array(
[0] = array(
[uid] = 1234
[page_id] = 4321
[type] = APPLICATION
)
)
FQL page_fan details here.