facebook: PHP graph api getting NULL session - facebook

I am following this tutoril. Though the user is logged in into facebook, but the session is showing him not logged in(NULL session). Please someone help me out. My code-segment is as follows-
$facebook = new Facebook(
array(
'appId' => $app_config['app_id'],
'secret' => $app_config['app_secret'],
'cookie' => true,)
);
$session = $facebook->getSession();
var_dump($session);
$fbme = null;
if ($session) {
try {
$uid = $facebook->getUser();
$fbme = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
}

I had the same situation, changed to iFrame instead of FBML, it worked.

Related

fb login error in php

I am creating fb login with the php ,
the problem is that the email of user is not returning from facebook,
FBID and USER NAME is returning.
The same code i have done for another project is working but for the new one is not working
the code is like this
require 'src/facebook.php'; // Include facebook SDK file
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXX', // Facebook App ID
'secret' => 'XXXXXXXXXXXXXXXXX', // Facebook App Secret
'cookie' => true,
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
$fbid = $user_profile['id']; // To Get Facebook ID
$fbuname = $user_profile['username']; // To Get Facebook Username
$fbfullname = $user_profile['name']; // To Get Facebook full name
$femail = $user_profile['email']; // To Get Facebook email ID
/* ---- Session Variables -----*/
$_SESSION['FBID'] = $fbid;
$_SESSION['USERNAME'] = $fbuname;
$_SESSION['FULLNAME'] = $fbfullname;
$_SESSION['EMAIL'] = $femail;
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
header("location:fblogin.php");
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email', // Permissions to request from the user
));
header("location: " . $loginUrl);
}
$user_profile = $facebook->api('/me?fields=name,email');
It´s called "Declarative Fields", see changelog. Btw, you can´t get the username anymore.

Facebook Group API Invite member

I've been trying all day to figure this out, but apparently it does not work for me.
What i'm looking into doing is ... ask for facebook login, and then add that user to a group ( send an invite ). I've tried to follow the API docs here:
https://developers.facebook.com/docs/reference/api/group/#invite_member
No luck.
My code looks like:
<?php
require "fb/facebook.php";
$access_token = "app_token_here";
$facebook = new Facebook(array(
'appId' => 'appId',
'secret' => 'secretBlabla',
'cookie' => true,
'allowSignedRequest' => false,
));
$params = array('scope' => 'read_friendlists,read_insights,user_about_me,user_birthday,user_groups,user_interests,user_likes,user_location,user_relationships,user_website');
$fuser = $facebook->getUser();
if ($fuser) {
try {
$user_profile = $facebook->api("/me");
} catch (FacebookApiException $e) {
error_log($e);
$fuser = null;
}
}
if ($fuser) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl($params);
}
$page = "/XXXXXX/members/".$fuser;
$token=$facebook->getAccessToken();
$facebook->api($page, "POST", array("access_token"=>$access_token)); // i've tried with $token as well
Error i get:
PHP Fatal error: Uncaught OAuthException: (#3) Unknown method\n
Any idea what i'm doing wrong ?
Thank you

Facebook GetUser() returns 0 without any changes of my code

I have user login implemented on my website. This was working good until now. When i try to login with Facebook account i always get 0 from GetUser(). I tryed with different web browser, updated PHP SDK to latest version, but still not working. Any clue ?
$facebook = new Facebook(array(
'appId' => 'my APP ID',
'secret' => 'my Secret',
'cookie' => false, // enable optional cookie support
));
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
$likes = $facebook->api("/me/likes/ID HIDDEN");
if (!isset($_SESSION['prijavljen'])) {
$_SESSION['prijavljen'] = True;
}
if (!empty($likes['data'])) $fblike = true; else $fblike = false;
} 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();
$logoutUrl = $facebook->getLogoutUrl(array( 'next' => 'my URL'));
} else {
$params = array('scope' => 'email,user_birthday,user_education_history,user_hometown,user_likes', 'redirect_uri' => curPageURL());
$loginUrl = $facebook->getLoginUrl($params);
}
Was the PHP version on your server recently updated? You can try this fix and see if getUser() function starts working for you again.

Get the facebook accounts value in a particular file

I have used the facebook php sdk and I want to get the value in another file, so I give the redirection path, after login it is going on the page but $user is blank, when I am refreshing the page then I got the values. my code is like below
From where the login page is calling :
$facebook = new Facebook(array('appId' =>FB_APP_ID ,'secret' => FB_APP_SECRET));
$scope='email,user_birthday,user_location,user_work_history,user_about_me,user_hometown';
$parameters=array("redirect_uri"=>"http://www.xyz.com//test.php","scope"=>$scope);
$login_url= $facebook->getLoginUrl($parameters);
and the test.php is like below
$facebook = new Facebook(array(
'appId' => '224402951020359' ,
'secret' => 'd2731260cdd7425dc11eee5f265a3dee'
));
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_data = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
print_r($user_data);
How can I get the value in first time.
On your test.php page, put a test for !$user and if true then redirect again to the loginurl... something like the following is what I do in my code to avoid this same problem.
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'email,publish_stream,user_birthday,user_location'
)
);
if ($user) {
try {
//get user basic description
$userInfo = $facebook->api("/$user");
$fb_access_token = $facebook->getAccessToken();
} catch (FacebookApiException $e) {
//you should use error_log($e); instead of printing the info on browser
error_log('Sharebox: '.$e); // d is a debug function defined at the end of this file
$user = null;
}
}
if (!$user) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
Hope this helps.

facebook connect redirect url suddenly broke

Before a minute ago after successful login it redirected to site.com/home, but now the link is site.com/home?state={long-code}6&code={long_code}
And this is ugly. Any ideas?
Update
This is the code i use :
session_start();
require 'facebook-php-sdk/src/facebook.php';
$facebook = new Facebook(array(
'appId' => '***',
'secret' => '***',
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'email,offline_access,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown',
'redirect_uri' => 'site.con/home'
)
);
$logoutUrl = $facebook->getLogoutUrl();
//session_destroy();
// Show user data
print_r($user_profile);
Update the sdk files you are using.this error comes from this file base_facebook.php this is now rectified in the new file.
Use this link for getting the latest SDK link