facebook api and fql qry post to all my friends - facebook

I want to publish All My Friends through apps
When I put this code it brings me an error
Parse error: syntax error, unexpected T_STRING, expecting ')' in test111.php on line 19
Can anyone help me?
Thank you
require "../settings.php";
require '../src/facebook.php';
require "../fbaccess.php";
require "web_config.php";
//user data from database
$sql = mysql_query("SELECT user_id,user_access_token,user_access_token_expire FROM data_table");
while ($data = mysql_fetch_array($sql)){
$user_id = $data['user_id'];
$accessToken = $data['user_access_token'];
$accessTokenExpire = $data['user_access_token_expire'];
$all_friends = $facebook->api(array('method' => 'fql.query', 'query' => 'SELECT uid FROM user WHERE (uid IN (SELECT uid2 FROM friend WHERE uid1=me()));
try {
$publishStream = $facebook->api("/$all_friends/feed", 'post', array(
'message' => ".",
'link' => '',
'picture' => '',
'name' => 'iOS Apps & Games',
'description'=> 'Checkout iOS apps and games from iThinkdiff.net. I found some of them are just awesome!'
)
);
//as $_GET['publish'] is set so remove it by redirecting user to the base url
} catch (FacebookApiException $e) {
error_log($e);
}
}
>?

You need one more ) at the end of the line that begins $all_friends=
You've got several other problems here. Try a PHP syntax checker like this: http://www.piliapp.com/php-syntax-check/

Related

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?

OAuthException : An active access token must be used to query information about the current user

I'm trying to run the sample code for Facebook connect that I downloaded from http://thinkdiff.net/facebook/php-sdk-3-0-graph-api-base-facebook-connect-tutorial/
I get this error message :
[error] => Array
(
[message] => An active access token must be used to query information about the current user.
[type] => OAuthException
[code] => 2500
)
You can try on my website : http://facebook.oliverjordan.net/thinkdiff
Here is the fbmain.php code :
<?php
//facebook application
$fbconfig['appid' ] = "463007877113xxx";
$fbconfig['secret'] = "-sensor-";
$fbconfig['baseurl'] = "http://facebook.oliverjordan.net/thinkdiff/index.php"; //"http://thinkdiff.net/demo/newfbconnect1/php/sdk3/index.php";
//
if (isset($_GET['request_ids'])){
//user comes from invitation
//track them if you need
}
$user = null; //facebook user uid
try{
include_once "facebook.php";
}
catch(Exception $o){
error_log($o);
}
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true,
));
//Facebook Authentication part
$user = $facebook->getUser();
// We may or may not have this data based
// on whether the user is logged in.
// If we have a $user id here, it means we know
// the user is logged into
// Facebook, but we don’t know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'email,offline_access,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown',
'redirect_uri' => $fbconfig['baseurl']
)
);
$logoutUrl = $facebook->getLogoutUrl();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
//you should use error_log($e); instead of printing the info on browser
d($e); // d is a debug function defined at the end of this file
$user = null;
}
}
//if user is logged in and session is valid.
if ($user){
//get user basic description
$userInfo = $facebook->api("/$user");
//Retriving movies those are user like using graph api
try{
$movies = $facebook->api("/$user/movies");
}
catch(Exception $o){
d($o);
}
//update user's status using graph api
//http://developers.facebook.com/docs/reference/dialogs/feed/
if (isset($_GET['publish'])){
try {
$publishStream = $facebook->api("/$user/feed", 'post', array(
'message' => "I love thinkdiff.net for facebook app development tutorials. :)",
'link' => 'http://ithinkdiff.net',
'picture' => 'http://thinkdiff.net/ithinkdiff.png',
'name' => 'iOS Apps & Games',
'description'=> 'Checkout iOS apps and games from iThinkdiff.net. I found some of them are just awesome!'
)
);
//as $_GET['publish'] is set so remove it by redirecting user to the base url
} catch (FacebookApiException $e) {
d($e);
}
$redirectUrl = $fbconfig['baseurl'] . '/index.php?success=1';
header("Location: $redirectUrl");
}
//update user's status using graph api
//http://developers.facebook.com/docs/reference/dialogs/feed/
if (isset($_POST['tt'])){
try {
$statusUpdate = $facebook->api("/$user/feed", 'post', array('message'=> $_POST['tt']));
} catch (FacebookApiException $e) {
d($e);
}
}
//fql query example using legacy method call and passing parameter
try{
$fql = "select name, hometown_location, sex, pic_square from user where uid=" . $user;
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$fqlResult = $facebook->api($param);
}
catch(Exception $o){
d($o);
}
}
function d($d){
echo '<pre>';
print_r($d);
echo '</pre>';
}?>
Can anyone help me?
You must ensure that your access token is active. Or maybe you're in logout condition. Or try to clean the cookies and cache from your browser (ctrl+shift+del)
Please check if you are getting any Facebook exceptions like
An active access token must be used to query information about the
current user.
which causes 0 as user id in return from $facebook->getUser().
So please check with getAccessTokenFromCode() from base_facebook.php and verify access_token_response format as it may be in JSON format.
So try to use appropriate decoding method to get $response_params['access_token'] from $access_token_response.

Facebook PHP SDK - An active access token must be used to query information about the current user

I have problem with Facebook PHP SDK. It always throws that exception. I tried many solutions listed here, but nothing works for me.
It seems that Facebook returns to me valid access token, because I tested it with Debug tool in dashboard of my application.
What's my scenario?
I want to post to publish simple content to user's wall by calling static function:
function social_publish($network, $title, $message, $link = '', $image = '') {
global $_config;
// Initialize Facebook SDK
$facebook = new Facebook(array(
'appId' => $_config['fb_app']['app_id'],
'secret' => $_config['fb_app']['app_security_key']
));
// Set data
$attachment = array(
'name' => $title,
'caption' => $title,
'message' => $message,
'link' => $link,
'picture' => $image,
'actions' => array('name' => 'Test', 'link' => 'Link')
);
try {
$access_token = $facebook->getAccessToken(); // returns valid access token
$uid = $facebook->getUser(); // always return 0
$result = $facebook->api( '/' . $_config['fb_profile'] . '/feed/', 'post', $attachment); // $_config['fb_profile'] procudes 'me' in this case
} catch (FacebookApiException $e) {
echo $e->getMessage();
}
}
Just to note: I am not working on local environment.
problem solve it as asked in scope request to Facebook for authentication, as I decided to use the JavaScript SDK-and then, here's the solution:
FB.getLoginStatus(function(response) {
if ( ! response.authResponse ) {
FB.login(function(response) {
// handle the response if needed
}, {scope: 'publish_actions, publish_stream'});
}
});
Thank you! :-)

How do I check if a given user ID has authenticated my app?

I'm using the Facebook Graph API and want to check if a user has authenticated my Facebook app by user ID. How do I do this?
You use:
SELECT is_app_user FROM user WHERE uid=USER_ID
This should return:
[
{
"is_app_user": true
}
]
If the user has logged in to your application.
Expanding on ifaour's answer, in PHP this query would look something like this:
<?php
$facebook = new Facebook(
'appID' => YOUR_APP_ID,
'secret' => YOUR_APP_SECRET
);
$result = $facebook->api(array(
'method' => 'fql.query',
'query' => "SELECT is_app_user FROM user WHERE uid=$user_id"
));
$is_installed = $result[0]['is_app_user'];
Here you can batch multiple requests together and avoid using FQL.
Assuming you have already logged into facebook and set the access token to the application access token, you can do this:
$batch = array();
foreach($friendArray AS $friend) {
$batch[] = array(
'method' => 'GET',
'relative_url' => '/' . $friend . '?fields=installed'
);
}
FB()->useApplicationAccessToken();
$batchResponse = FB()->facebook()->api('?batch='.json_encode($batch), 'POST');
Then you can process the batch response with code like this:
$installedUsers = array();
$notInstalledUsers = array();
foreach ($batchResponse AS $response) {
$body = json_decode($response['body'], true);
if (!isset($body['id']))
continue;
$id = $body['id'];
if (isset($body['installed']))
$installedUsers[] = $id;
else
$notInstalledUsers[] = $id;
}

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.