Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

facebook get user birthday

Tags:

php

facebook

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

like image 540
Jeremy Gwa Avatar asked Dec 07 '25 12:12

Jeremy Gwa


2 Answers

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

  • lines in an IF BLANK, do not display field.
  • 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.

    like image 106
    ifaour Avatar answered Dec 10 '25 02:12

    ifaour


    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/

    like image 37
    AjayR Avatar answered Dec 10 '25 01:12

    AjayR



    Donate For Us

    If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!