Christoffer Christoffer - 6 months ago 72
PHP Question

Get user birthday Facebook Graph

I want to collect some information when a user log in through Facebook.
I'm able to get all the information i need except the users birthday

if ( isset( $session ) ) {
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me?fields=first_name,last_name,birthday,email,gender');
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
$fbid = $graphObject->getProperty('id'); // To Get Facebook ID
$fbfname = $graphObject->getProperty('first_name'); // To Get Facebook first name
$fblname = $graphObject->getProperty('last_name'); // To Get Facebook last name
$fbbday = $graphObject->getProperty('birthday'); // To Get Facebook birthday
$femail = $graphObject->getProperty('email'); // To Get Facebook email ID
$fgender = $graphObject->getProperty('gender'); // To Get Facebook gender
/* ---- Session Variables -----*/
$_SESSION['FBID'] = $fbid;
$_SESSION['FIRST_NAME'] = $fbfname;
$_SESSION['LAST_NAME'] = $fblname;
$_SESSION['BIRTHDAY'] = $fbbday;
$_SESSION['EMAIL'] = $femail;
$_SESSION['GENDER'] = $fgender;
checkuser($fbid,$fbfname,$femail);
/* ---- header location after session ----*/
header("Location: index.php");
} else {
$loginUrl = $helper->getLoginUrl(array('scope' => 'email'));
header("Location: ".$loginUrl);
}
?>


What am I doing wrong?

Answer

You donĀ“t ask for the correct permission:

$loginUrl = $helper->getLoginUrl(array('scope' => 'email,user_birthday'));

Btw, make sure to test this with an App Admin. user_birthday needs to get reviewed before everyone can use it: https://developers.facebook.com/docs/facebook-login/review