I am doing a registration on my website via facebook.
When the user logs in via facebook the $user array returned is not exactly what i want.
I have gone through the user parameters that are accessible via facebook, i have tried implementing them also but it is not working.
This is a sample of what i have
require_once "Database_Connect.php";
if (!isset($_POST['choosepassword']))
{
# We require the library
$user=array();
require("facebook.php");
# my error tracker
$error=0;
# Creating the facebook object
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
'cookie' => true
));
# Let's see if we have an active session
$session = $facebook->getSession();
if(!empty($session)) {
# Active session, let's try getting the user id (getUser()) and user info (api->('/me'))
try{
$uid = $facebook->getUser();
$user = $facebook->api('/me');
} catch (Exception $e){}
if(!empty($user)){
# User info ok? Let's print it (Here we will be adding the login and registering routines)
print_r($user);
***At this point what is retrieved is not exactly what i want*****
$ue=$user['email'];$ui=$user['id'];
$query = mysql_query("select * from members where email = '$ue' or (oauth_provider = 'facebook' AND oauth_uid = '$ui')", $link);
$result = mysql_fetch_array($query);
# If not, let's add it to the database
if(!empty($result)){
$error = 2; //record already in database
require_once "facebook_error.php";
die();
}
} else {
# For testing purposes, if there was an error, let's kill the script
$error = 1; //we were unable to retrieve info frm facebook
require_once "facebook_error.php";
die();
}
} else {
# There's no active session, let's generate one
$login_url = $facebook->getLoginUrl();
*** I tried specifying what i want returned here, but it doesnt seem to work*****
$url = $facebook->getLoginUrl(array(
'req_perms' => 'uid, first_name, last_name, name, email, current_location, user_website, user_likes, user_interests, user_birthday, pic_big',
'next' => 'http://www.zzzzzzz.com/facebook_register.php',
'cancel_url' => 'http://www.zzzzzzz.com'
));
header("Location: ".$login_url);
}
What am i not doing right?
Thank You
Update
I am using FQL to select user info from facebook now,
$fql = "select uid, first_name, last_name, name, sex, email, current_location, website, interests, birthday, pic_big from user where uid=me()";
$param = array('method' => 'fql.query', 'query' => $fql, 'callback' => '');
$user = $facebook->api($param);
It retrieves all the data except the birthday and the email
How can i select the email and birthday?
Thanks
Your application needs the user_birthday and email permissions for this, else it will not return that information. You only need to supply parameters that need a permission, not what fields you want in the req_perms parameter, so it should look like this:
$url = $facebook->getLoginUrl(array(
'req_perms' => 'email, user_birthday',
'next' => 'http://www.zzzzzzz.com/facebook_register.php',
'cancel_url' => 'http://www.zzzzzzz.com'
));
Related
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.
I use facebook sdk api with symfony, in the below $user_profile array he can retrieve every information but email and birthday.
require 'facebook.php';
$app_id = sfConfig::get('app_facebook_app_id');
$app_secret = sfConfig::get('app_facebook_secret_key');
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$location= $user_profile['location']['name'];
$gender = $user_profile['gender'];
$user_info = array(
'uid' => $user_profile['id'],
'first_name' => $user_profile['first_name'],
'last_name' => $user_profile['last_name'],
'email' => $user_profile['email'],
'birthday' => $user_profile['birthday'],
'gender' => $user_profile['gender'],
);
when I use print_r($user_profile) it return:
Array (
[uid] => 100004246655890
[first_name] => Ala
[last_name] => Hamad
[email] =>
[birthday] =>
[gender] => male
)
the email and birthday empty.
Nowhere in your code does it appear that you're requesting the Permission needed to access the user's email address or birthday
Check the permissions doc and make sure you're requesting the email and user_birthday permissions in your Authentication code.
If you don't receive these information, one of the cause is because the user configuration prohibits sharing them and Facebook don't send the warns.
Because some users, in the configuration, prohibits sharing the email or other information.
I searched for how to write that in code, then I found that I have to request email permission within the scope in the login url:
$params = array(
'redirect_uri' => url_for('sfGuardAuth/loginWithFB', true),'scope' => 'email,user_birthday,user_location'
);
$loginUrl = $facebook->getLoginUrl($params);
I have a facebook login script for my site that currently works fine. I want to add the ability to request the email from user, along with the current basic info. I know i need to request an access token, but icant quite figure out how. Here's my current code:
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret' => APP_SECRET,
'cookie' => true
));
$session = $facebook->getSession();
if (!empty($session)) {
# Active session, let's try getting the user id (getUser()) and user info (api->('/me'))
try {
$uid = $facebook->getUser();
$user = $facebook->api('/me/');
You have to provide an extended permission for email at the time of login..
$access_token = $this->facebook->getAccessToken();
//check permissions list
$permissions_list = $this->facebook->api('/me/permissions', 'GET', array('access_token' => $access_token));
$permissions_needed = array('email');
foreach ($permissions_needed as $perm) {
if (!isset($permissions_list['data'][0][$perm]) || $permissions_list['data'][0][$perm] != 1) {
$login = $facebook->getLoginUrl(array('scope' => 'email,user_birthday',
'redirect_uri' => your site redirectUri,
'display' => 'popup'
));
header("location:$login");
}
}
$user = $facebook->getUser();
if ($user) {
try {
$userInfo = $facebook->api("/$user");
} catch (FacebookApiException $e) {
echo $e->getMessage();
}
}
print_r($userInfo);
Have a look at the server-side authentication process - this is where you pass through the permissions you want (known as "scope"). The user will be presented with a login panel if they are not already logged in, and then afterwards a separate permissions panel for any permissions above basic you are requesting and that they haven't already granted. You will then receive an auth token or a code that can be exchanged for a token, and you use this to then further query the user and get their email details in the response.
Good luck!
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;
}
I am trying to work out facebook connect based on this tutorial:
http://net.tutsplus.com/tutorials/php/how-to-authenticate-your-users-with-facebook-connect/
They have given you a few scripts to use to figure it out, this is the extended permissions one:
<?php
# We require the library
require("facebook.php");
# Creating the facebook object
$facebook = new Facebook(array(
'appId' => '...',
'secret' => '...',
'cookie' => true
));
# Let's see if we have an active session
$session = $facebook->getSession();
if(!empty($session)) {
# Active session, let's try getting the user id (getUser()) and user info (api->('/me'))
try{
$uid = $facebook->getUser();
# req_perms is a comma separated list of the permissions needed
$url = $facebook->getLoginUrl(array(
'req_perms' => 'email,user_birthday,status_update,publish_stream,user_photos,user_videos'
));
header("Location: {$url} ");
} catch (Exception $e){}
} else {
# There's no active session, let's generate one
$login_url = $facebook->getLoginUrl();
header("Location: ".$login_url);
}
When I execute this it lets me allow the extended permissions but then I get a redirect loop error. When I check in facebook it has granted extended permissions.
Now I tried to just implement it into the login script they gave, which is this
# We require the library
require("facebook.php");
# Creating the facebook object
$facebook = new Facebook(array(
'appId' => '...',
'secret' => '...',
'cookie' => true
));
# Let's see if we have an active session
$session = $facebook->getSession();
if(!empty($session)) {
# Active session, let's try getting the user id (getUser()) and user info (api->('/me'))
try{
$uid = $facebook->getUser();
$user = $facebook->api('/me');
} catch (Exception $e){}
if(!empty($user)){
# We have an active session, let's check if we have already registered the user
$query = mysql_query("SELECT * FROM users WHERE oauth_prov = 1 AND oauth_id = ". $user['id']);
$result = mysql_fetch_array($query);
# If not, let's add it to the database
echo $user['id'] . $user['name'];
if(empty($result)){
$query = mysql_query("INSERT INTO users (oauth_prov, oauth_id, username) VALUES ('1', {$user['id']}, '{$user['name']}')");
$query = mysql_query("SELECT * FROM users WHERE id = " . mysql_insert_id());
$result = mysql_fetch_array($query);
}
// this sets variables in the session
$_SESSION['id'] = $result['id'];
$_SESSION['oauth_uid'] = $result['oauth_id'];
$_SESSION['oauth_provider'] = $result['oauth_provider'];
$_SESSION['username'] = $result['username'];
} else {
# For testing purposes, if there was an error, let's kill the script
die("There was an error.");
}
} else {
# There's no active session, let's generate one
$login_url = $facebook->getLoginUrl();
header("Location: ".$login_url);
}
However, I don't know where it should go, as I would like to request permissions at the same time as the login to get the users details. If I put this:
try{
$uid = $facebook->getUser();
# req_perms is a comma separated list of the permissions needed
$url = $facebook->getLoginUrl(array(
'req_perms' => 'email,user_birthday,status_update,publish_stream,user_photos,user_videos'
));
header("Location: {$url} ");
} catch (Exception $e){}
After I set the session values then I don't even get to allow anything, it just hangs and I get a redirect error.
If I put it just after getting the facebook user_id then it gives me the allow dialog (although one after the other as its obviously asking twice, something I would like to stop) but then again gives me a redirect error, having granted the extended permissions
I really don't have a clue where to put it, the documentation for facebook is absolutely abysmal
Instead of this:
# req_perms is a comma separated list of the permissions needed
$url = $facebook->getLoginUrl(array('req_perms' => 'email,user_birthday,status_update,publish_stream,user_photos,user_videos'));
try:
$url = $facebook->getLoginUrl(array('scope' => 'email,user_birthday,status_update,publish_stream,user_photos,user_videos'));
for an complete example see the php-sdk:
https://github.com/facebook/php-sdk/blob/master/examples/example.php