// Function for facebook login
function facebook_login() {
$output = array();
// Create facebook object for user login.
$config = array();
$config['appId' ] = 'XXXXXXX';
$config['secret'] = 'XXXXXXX';
$config['cookie'] = true;
session_start();
$code = $_GET['code'];
$facebook_user = null;
// Create the facebook user object
$facebook = new Facebook(array(
'appId' => $config['appId'],
'secret' => $config['secret'],
'cookie' => $config['cookie'],
));
// Get User ID
$facebook_user = $facebook->getUser();
// If facebook user id not equal to 0
if ($facebook_user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
$output['facebookurl'] = 'user/register';
return $output; // Returns a result.
}
catch (FacebookApiException $e) {
drupal_set_message($e, 'error');
$facebook_user = null;
}
}
else {
$facebook_params = array(
'scope' => 'email',
'redirect_uri' => 'http://localhost/mywebsite',
'display' => 'popup',
'code' => $code,
);
$output['facebookurl'] = $facebook->getLoginUrl($facebook_params); // Returns the facebook login url as result.
}
}
Here is my php code for facebook login. Certain event will call the facebook_login()
function and will do some processing based on the $output
result.
The problem is $facebook_user
is always 0, i.e. I always get the getLoginUrl
as output. I have tried all the answers in on stackoverflow related to this issue, but none helped me.
I am testing this app on my localhost and have set the Site URL to http://localhost/mywebsite
in facebook app settings.
Please help!