facebook get user birthday
All,
I have been struggling to retrieve a users开发者_运维知识库 birthday, via PHP.
I look on the net and all the examples given do not work, or have been depreciated.
what is the code to get a users birthday ...eg. Year day month ?
some clear code would be useful
regards,
Jer
This uses PHP SDK v4, not v5, however I found it easier to follow, and this persons tutorial is fantastic. http://www.krizna.com/demo/login-with-facebook-using-php/
They explain how to retrieve EMAIL in the comments, and if you update it as follows you can get GENDER (Still learning myself so not sure what other fields applies to these steps)
In your index.php, update the following:
<?php if ($_SESSION['FULLNAME'] !="") {
?>
<li class="nav-header">Name</li>
<li><?php echo $_SESSION['FULLNAME']; ?></li>
<?php
} ?>
<?php if ($_SESSION['EMAIL'] !="") {
?>
<li class="nav-header">Email</li>
<li><?php echo $_SESSION['EMAIL']; ?></li>
<?php
}
?>
<?php if ($_SESSION['GENDER'] !="") {
?>
<li class="nav-header">Gender</li>
<li><?php echo $_SESSION['GENDER']; ?></li>
<?php
}
?>
The above code looks slightly different from the demo code because I wrapped the
In the FBConfig.php file, update the following:
// see if we have a session
if ( isset( $session ) ) {
// graph api request for user data
// $request = new FacebookRequest( $session, 'GET', '/me' );
$request = new FacebookRequest( $session, 'GET', '/me?locale=en_US&fields=id, name, email, gender, birthday' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
$fbid = $graphObject->getProperty('id'); // To Get Facebook ID
$fbfullname = $graphObject->getProperty('name'); // To Get Facebook full name
$fbemail = $graphObject->getProperty('email'); // To Get Facebook email ID
$fbgender = $graphObject->getProperty('gender'); // To Get Facebook gender ID
$fbbirthday = $graphObject->getProperty('birthday'); // To Get Facebook birthday ID
/* ---- Session Variables -----*/
$_SESSION['FBID'] = $fbid;
$_SESSION['FULLNAME'] = $fbfullname;
$_SESSION['EMAIL'] = $fbemail;
$_SESSION['GENDER'] = $fbgender;
$_SESSION['BIRTHDAY'] = $fbbirthday;
Birthday isn't working for me like this, but I believe it might be for a few reasons such as additional permissions are needed, I have mine hidden, etc. Hope this helps.
You should have the login button code which should look like
<fb:login-button autologoutlink="true" perms="email,user_birthday,status_update,publish_stream"></fb:login-button>
To read the user details, you need to use PHP facebook API which can be downloaded freely from Facebook.
The following link will explain you clearly further.
http://thinkdiff.net/facebook/php-sdk-graph-api-base-facebook-connect-tutorial/
Your app must request the user_birthday
(or friends_birthday
) permission in order to get this.
Reference:
- https://developers.facebook.com/docs/reference/api/user/
- https://developers.facebook.com/docs/authentication/permissions/
精彩评论