Using facebook javascript sdk to process fql query - facebook

I have just put implemented Facebook connect on my web-site using the JavaScript SDK. Now I would like to process an fql query.
So I have two questions:
Is it possible using only JavaScript fql as "SELECT id, name FROM user WHERE uid IN (SELECT uid1 FROM friends WHERE uid2=me())" ?
Because when I tried some PHP SDK code using the facebook doc
PHP:
$app_id = 'MY_ID';
$app_secret = 'MY_APP_SECRET';
$my_url = 'POST_AUTH_URL';
$code = $_REQUEST["code"];
$_REQUEST["code"] is quite normally not recognized and I don't know what is the "'POST_AUTH_URL'" because I didn't use the PHP SDK to process the Facebook-connect. Does anyone have an idea?

Using method: 'fql.query' is part of the deprecated REST API.
A better, future-proof way IMHO is to send your queries against the Graph API:
FB.api("/fql?q={your urlencoded query}", callback() { … } );

Using the JavaScript SDK you can easily execute an FQL query -
FB.api(
{
method: 'fql.query',
query: 'SELECT name FROM user WHERE uid=me()'
},
function(response) {
alert('Your name is ' + response[0].name);
}
);
Don't forget to request all the permissions you need before executing any queries...
Reference - https://developers.facebook.com/docs/reference/javascript/

Sorry I can't comment yet but to clarify this helpful answer; what worked for me was formatting the FQL API call the same way I would a Graph API call with {fields: "ex1,ex2"}:
var fqlRequest = "SELECT name FROM user WHERE uid = me()";
FB.api("/fql",{q:fqlRequest}, function(response){
// do cool stuff with response here
});
Note that I did NOT have to URL encode the string and it worked fine for me (I had used
encodeURI(fqlRequest) before and it returned an unexpected % error).
Also remember to quote literals if you use them:
var id = "1234567890";
var fqlRequest = "SELECT name FROM user WHERE uid = ' " + id + " ' ";

Related

Laravel, Facebook API: retrieving friends of friends

In a previous thread we were strugling with the Facebook login using a Laravel app. Now, as the user logs into our app using Facebook, we are trying to get his friend's list in order to provide some qualified suggestions. So, here's what we are trying:
Route::get('login/fb/callback', function() {
// A FacebookResponse is returned from an executed FacebookRequest
$helper = new FacebookRedirectLoginHelper('http://yoururl/login/fb/callback');
$session = $helper->getSessionFromRedirect();
$request = new FacebookRequest($session, 'GET', '/me/friends');
try {
$response = $request->execute();
$me = $response->getGraphObject();
} catch (FacebookRequestException $ex) {
echo $ex->getMessage();
} catch (\Exception $ex) {
echo $ex->getMessage();
}
print_r($me);//->getProperty("id"));
$user = new Giftee;
$user->name = $me->getProperty('first_name') . ' ' . $me->getProperty('last_name');
$user->email = $me->getProperty('email') . "#facebook.com.br";
$user->photo = 'https://graph.facebook.com/' . $me->getProperty('id') . '/picture?type=large';
$user->save();
});
As a result, we get:
object(Facebook\GraphObject)#146 (1) { ["backingData":protected]=> array(0) { } }
By reading the Facebook API docs, they pretty much say user_friends will only return friends of friends that already use your app and We could not see any login permission that would just return friends of friends (not only the ones using our app). In another words, is what we want really impossible?
You're not able to retrieve friends of friends. With Graph API v2.0, you even only get those friends of your app's users which also use your app.
And, all friends_* permissions have been removed. So I don't see a chance for you to implement what you want to achieve.

Fetching FB user-id returns incorrect value ("1")

I have a very strange problem:
Sometimes (but not every time!) when I try to get the current user facebook ID, I get "1";
for example:
$config = array();
$config["appId"] = API_KEY;
$config["secret"] = SECRET;
$facebook= new Facebook($config);
$user=$facbook->getUser();
echo $user['id'];//returns 1;
Obviously - this it not the real user ID.
When I check if the access token is still good -I see that it is still valid. Also, even if I force the user to login again using:
FB.login(function(response) {
if (response.authResponse) {
top.location.href="<?php echo HOME_URL;?>"
}, {scope: 'publish_stream'});
Still getting the same result.
Any idea what could be the problem?
You should ONLY get an ID back from FB:
$user_id = $facbook->getUser();
echo $user_id;
You would later have to query the API for a user profile (which is what it looks like you are doing):
$user_profile = $facebook->api('/me');
Reference:
http://developers.facebook.com/docs/reference/php/facebook-api/

need help Displaying more than 1 response from FQL query is_app_user Or Similar solutions

I am currently trying to get a list of friends who have added the same app via the FQL query.
I am actually doing this for a school project, and apparently, the server where this is going to be saved is not supported via PHP.That is why i am using the JavaScript SDK to code this.
This is actually the first time that I am doing a project in Facebook and everything is extremely new to me and having the graph api is not helping with me with any old documentations at all since many things have been changed and documentation is very scarce >.<.
Currently, I am having problems displaying multiple results (i am only able to do so if i hard code the response)
I've tried using a for loop to print the result but to no avail.
FB.api('/me', function(response) {
showLoader(false);
var query = FB.Data.query('SELECT uid, name, pic_square FROM user WHERE uid
IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND is_app_user');
query.wait(function(rows) {
for(var i = 0; i< rows.length;i++)
{
document.getElementById('debug').innerHTML =
''+ rows[i].name +"<br />" + "<img src=\""+ rows[i].pic_square +"\" /><br />";
}
});
});
am i doing this wrong in any way? >.<. Any help would be reallllly great!!
Thanks!
Maybe it is the fact that you are stepping over what you are placing in the debug div. Try
document.getElementById('debug').innerHTML +=
Also, you do not have to have a for loop. You can have facebook do it for you by using their forEach function.
To get multiple results returned, I do this:
var linkquery = FB.Data.query('select owner, owner_comment, url from link where owner={0}', response.id);
FB.Data.waitOn([linkquery], function() {
FB.Array.forEach(linkquery.value, function(row) {
document.getElementById('debug').innerHTML += 'FQL Information: '+ "<br />" +
'Your name: ' + row.owner + "<br />" +
'Your comment: ' + row.owner_comment + "<br />" +
'Your link: ' + "<a href='" + row.url + "'>" + row.url + "</a><br /><br />";
});
});
The FB.Array.forEach gets each row.owner and they are being added to the debug div -
document.getElementById('debug').innerHTML +=
with each row that is returned.
Hope that helps

facebook share count - problem with FQL with different URLs

I have a plugin on my page which allows users to share articles on my site on facebook, but each url is appended with their user id.
What I want to do is count the number of total share regardless of the shareers user id.
However I cannot get the FQL to work as the where parameter will not accept the LIKE function..
the pages will be something like:
page1.php?sid=2
page1.php?sid=34
page1.php?sid=12
But I want to retrieve the total shares for page1.php regardless of the sid.
The LIKE function doesnot work in FQL does anyone have any ideas as I am struggling
current code:
https://api.facebook.com/method/fql.que … ge1.php%27
You can use FQL to make your query :
$fql = 'SELECT total_count FROM link_stat WHERE url="http://google.com"';
$json = file_get_contents('https://api.facebook.com/method/fql.query?format=json&query=' . urlencode($fql));
$data = json_decode($json);
echo $data[0]->total_count;
Here, total_count gives you the number of shares for the link.
If you have several URL to query, you can make all of that in only one query by using OR :
SELECT url, total_count FROM link_stat WHERE url="..." OR url="..."
Here is an example is you want to get the number of shares for theses 3 URLs :
$urls = array(
"http://www.example.com/page1.php?sid=2",
"http://www.example.com/page1.php?sid=12",
"http://www.example.com/page1.php?sid=34",
);
function wrap($url) {
return 'url="' . $url . '"';
}
$fql = 'SELECT url, total_count FROM link_stat WHERE ';
$fql .= implode(" OR ", array_map("wrap", $urls));
$json = file_get_contents('https://api.facebook.com/method/fql.query?format=json&query=' . urlencode($fql));
$data = json_decode($json);
And $data is an array of 3 objects with the share number for each URL :
array(4) {
[0]=> object(stdClass)#2 (2) {
["url"]=> string(17) "http://www.example.com/page1.php?sid=2"
["total_count"]=> int(1318598)
}
[1] => ...
[2] => ...
[3] => ...
}
Then you just have to go through the array to make a sum.
Hope that helps !

How can I get friendship details in Facebook Graph API?

How can I get a friendship detail for two people? So for example in the web it will be:
http://www.facebook.com/<my_id>?and=<friend_id>
Is there any way I can do this in Graph API? Furthermore can I get specific items such as photos together, wall posts between us, etc? (Not documented AFAIK, but many Graph API features aren't anyway...)
EDIT: I think it should be possible with Graph API. For example getting family details (brother, sister, parents, etc) is not documented yet I still able to do it.
You can simulate a friendship query by doing multiple FQL queries. For example, to get all the photos between two friends A and B, you can:
Get all photos (p) from A
Get all tags of B in p (query on the photo_tags table)
Get all comments made by B on p (query on the comments table)
Repeat, this time selecting photos from B.
You can apply the same concept on other things such as posts, likes, videos etc.
Yes, I think you can also do the same thing phillee answered with the Graph API instead of FQL:
Get user's photos https://graph.facebook.com/USERID/photos
Get each photo's tags https://graph.facebook.com/PHOTOID/tags
Sort through the list of photo tags, and grab all photos with the Friend in them
Get each photo's comments https://graph.facebook.com/PHOTOID/comments
Sort through the list of photo comments, and grab all comments left by the friend
As the other answer also said: rinse and repeat for all data you want
https://graph.facebook.com/USERID/feed
https://graph.facebook.com/USERID/posts
etc etc, see all connections here: http://developers.facebook.com/docs/reference/api/user/
For the interests, movies, activities, etc just make an API call for both (https://graph.facebook.com/ONEUSER/music and https://graph.facebook.com/OTHERUSER/music) and find the intersection of the two sets of data (loop through each list and save all matches to a separate list of mutual Likes)
There are no specific API calls for friendships though, so you will have to build your own. My guess is that Facebook is doing exactly this on their Friendship pages :)
It should be just as easy with FQL as with the REST API... maybe even easier with FQL since you can add WHERE conditions and get back just the data you need (SELECT * FROM comments WHERE fromid = FRIENDID), instead of sorting through the big list returned by the API (unless there is a way to add conditions to API request URLs?).
If I understand properly you want to "View Friendship"
This type of query is not directly possible using the Graph API (at the moment). You would need to gather the information from each resource endpoint and then do some relating on your own part to "View Friendship"
This is what I have been using:
<?php
function main()
{
global $Fb;
$Fb = new Facebook(array('appId'=>FB_API_ID,'secret'=>FB_API_SECRET));
$logoutUrl = $Fb->getLogoutUrl();
$loginUrl = $Fb->getLoginUrl();
$user = fb_loguser($Fb);
if ($user)
{
$txt .= "<p align=\"center\">Logout</p>";
$txt .= "<h3 align=\"center\">You</h3>";
$access_token = $Fb->getAccessToken();
$user_profile = $Fb->api('/me');
$txt .= "<p align=\"center\">
<img src=\"https://graph.facebook.com/".$user."/picture\"></p>";
$txt .= fb_friends_list($user_profile,$access_token);
}
}
function fb_loguser($facebook)
{
global $Fb;
$Fb = $facebook;
$user = $Fb->getUser();
if ($user)
{
try
$user_profile = $Fb->api('/me');
catch (FacebookApiException $e)
{
error_log($e);
$user = null;
}
}
return($user);
}
function fb_friends_list($access_token)
{
global $Sess,$Fb;
$friends = $Fb->api('/me/friends'); // return an array [data][0 to n][name]/[id]
$siz = sizeof($friends['data']);
$txt = "<h3>Your FRIENDS on FACEBOOK</h3>";
$txt .= "<table>";
for ($i=0; $i<$siz; $i++)
{
$fid = $friends['data'][$i]['id'];
$src = "http://graph.facebook.com/".$fid."/picture";
$txt .= "<tr><td><img src=\"".$src."\" /></td>";
$txt .= "<td>".$friends['data'][$i]['name'] . "</td>";
$txt .= "<td>" . $fid . "</td></tr>";
}
$txt .= "</table>";
return($txt);
}
?>
Call main()!
Getting the photos where two (or more) users are tagged in:
SELECT pid, src_big FROM photo
WHERE pid IN(
SELECT pid FROM photo_tag WHERE subject=me())
AND pid IN(
SELECT pid FROM photo_tag WHERE subject=FRIEND_ID)
AND pid IN(
SELECT pid FROM photo_tag WHERE subject=ANOTHER_FRIEND_ID)
...
http://bighnarajsahu.blogspot.in/2013/03/facebook-graph-api-to-get-user-details.html
This is the complete code to get the friendlist in Fb.