how to use fql query - facebook-fql

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.

Related

Trying to post to facebook page with app but get "The user hasn't authorized the application to perform this action"

I am trying to write a script that will post an update to a facebook page timeline. I created an app and created a key for the app. I am using the code below:
require_once HOME_PATH . '/include/facebook/facebook.php';
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_APP_SECRET,
'cookie' => true,
'scope' => 'publish_stream',
));
$user_id = $facebook->getUser();
$access_token = $facebook->getAccessToken();
$attachment = array(
'access_token' => $access_token,
'message' => 'this is my message',
'name' => 'name',
'link' => ROOT_URL . $blog_data['url'] . '.htm',
'caption' => $blog_data['title'],
'description' => $blog_data['title'],
);
if ($image = get_first_image($blog_data['body'])) {
$attachment['picture'] = $image;
}
$facebook->api(FACEBOOK_BLOG_PAGE . '/feed', 'post', $attachment);
i think this key is connected to the app and not to my user, so it sounds like i would have to grant the permission within facebook, but i have seen some other posts telling me that i need to do this in code. The examples aren't very clear.
I know similar questions have been asked but i haven't seen any clear answers yet. Can anyone please clarify this?
If you want to get user_id by this piece of code: $user_id = $facebook->getUser();
The first thing you need, is to login an user through a URL returned by $facebook->getLoginUrl(array('scope' => array('perm', 'perm2'))).
Also, notice, where I put permissions, which I require. Not into the Facebook class constructor, but into the method getLoginUrl.
And lastly, for posting into a page, you need publish_actions permission (not publish_stream).

Facebook API pending requests

I know this have been asked several times, and I have already read several posts in here, but somehow it doesn't work for me. Even when I gave myself ALL permissions and not only 'read_requests'.
I have been trying with FQL
try {
$me = $facebook->api('/me');
$currentPage=1;
$currentPost=1;
$currentDate="";
$list="";
$arrDate=array();
$fql = "SELECT uid_from FROM friend_request WHERE uid_to = me()";
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$fqlResult = $facebook->api($param);
var_dump($fqlResult);
foreach($fqlResult as $row){
var_dump($row);
}
}
catch (FacebookApiException $e) {
echo 'error: <pre>';
var_dump($e);
}
And also in the Graph Explorer with the following line
MY_ID?fields=friendrequests.fields(created_time),name
But Graph Explorer says I don't have permission, even when I gave myself full permission. What can I be doing wrong?

Facebook: Returns "500 (Internal Server Error)" for friends info request

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/

facebook application : get birthday

I would like to know how to get the date of birthday of the user of facebook.
Here is the code which deal with facebook api :
<?php require 'src/facebook.php';
define("FB_APP_ID","***");
define("FB_SECRET","***");
$facebook = new Facebook(array(
'appId' => FB_APP_ID,
'secret' => FB_SECRET,
'cookie' => true,
));
$currentUser = $facebook->getUser();
if($currentUser) {
try
{
$facebook_profile = $facebook->api('/me');
$friends_fields = $facebook->api('/me/friends');
}
catch (FacebookApiException $e)
{
print_r($e);
$user = null;
}
}
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'user_birthday, birthday, date_birthday, email, offline_access, publish_stream'
)
);
$logoutUrl = $facebook->getLogoutUrl();
?>
I tried $facebook_profile['user_birthday'] and $facebook_profile['birthday']
But it does not work.
I don't know how to do ? Can you help me please ?
The correct permission for birthday is user_birthday
Verify you have the permission
$permissions = $facebook->api('/me/permissions');
$permissions["data"][0]["user_birthday"];
Seeing that you have facebook_profile, you should just dump the contents of that response to see what you do have.
Also ensure the user did set his/her birthday...
There's probably more than one way to do it, but you should be able to get the user's birthday from an FQL call via the SDK api() method:
$result = $facebook->api(array(
'method' => 'fql.query',
'query' => 'SELECT birthday_date FROM user WHERE uid = me()',
));
$birthday = $result[0]['birthday_date'];
You'll need to have user_birthday or friends_birthday permissions to read the birthday and birthday_date fields.

how to post on friends' walls using php sdk?

i am trying to post to my friends' feeds using this code, but it is not working . i am stuck, any help ??
$app_url ="http://localhost.local/PMS/facebook/PostWithPHP.php";
$facebook = new Facebook(array(
'appId' => 'APPID',
'secret' => 'APPSECRET',
'cookie' => true,
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
$user_friends = $facebook->api('/me/friends');
sort($user_friends['data']);
try {
// Proceed knowing you have a logged in user who's authenticated.
$access_token = $facebook->getAccessToken();
$vars = array(
'message' => 'My Message',
'name' => 'title',
'caption' => 'Caption',
'link' => 'Link',
'description' => 'Description',
'picture' => 'image'
);
foreach($user_friends['data'] as $f){
$sendTo = $f['id'];
$sendToName = $f['name'];
$result = $facebook->api("/".$sendTo ."/feed", 'post', $vars);
}
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array('redirect_uri'=> $app_url));
echo "<script type='text/javascript'>";
echo "top.location.href = '{$loginUrl}';";
echo "</script>";
}
and another question is that using this code, but with replacing $facebook->api("/".$sendTo ."/feed", 'post', $vars); by $facebook->api("/me/feed", 'post', $vars); and of course without looping friends, posts on my timeline. how can i make it post on my wall ??
I guess for a post to a timeline you will need an accessToken from the user where to publish the content. In your case you just have the accessToken of the registered user, not of his friends. That is a restriction by FB I think.
1st check you getting the user id(place echo and check) if not try this... I think this will help you brother
$token_url = "https://graph.facebook.com/oauth/access_token?" ."client_id=" . $app_id ."&client_secret=" . $app_secret .
"&grant_type=client_credentials";
$access_token = file_get_contents($token_url);
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
$user_id = $data["user_id"];
There are a few things wrong with your code. First of all, make sure the link and picture parameters for the post are valid URLs. Facebook will give you a error message otherwise ((#100) link URL is not properly formatted). Also, the link must go to the Canvas or Site URL for your application.
That should solve the issues with posting to a friend's wall.
However, may I remind you that your application is in violation of the Facebook Platform Policy. Facebook doesn't allow multiple posts to the stream (whether its yours or a friends) unless there is explicit permission from the user. It also to stop common friends seeing the same message from multiple friends.
You can follow the tutorial here:
https://www.webniraj.com/2012/11/22/facebook-api-posting-a-status-update/
But, instead of making a API call to: /me/feed, you replace me with the friend's User ID, so it looks like /12345/feed
Please note that Facebook has now disabled posting to Friends' walls via the API. Instead, you should either tag the user in an action or use the Requests API.