Getting user's birthday through Facebook API

16,825

Solution 1

You are logged in on the client side but php is executed on the server side so you receive only the basic information of an user object.

If you logged in on the server side and have the permissions so you can get the birthday simply with:

echo $facebookUser['birthday'];

Solution 2

Most probably you didn't receive approval to request user_birthday from FB.

You can't get user_birthday unless FB reviews your FB app.

Only the following permissions may be requested without review: public_profile, user_friends and email (See: https://developers.facebook.com/docs/facebook-login/review/what-is-login-review)

Without review you can request only age_range, but not user_birthday

Share:
16,825
Lumocra
Author by

Lumocra

Web and game developer @ YIPP

Updated on June 15, 2022

Comments

  • Lumocra
    Lumocra almost 2 years

    I'm having some troubles with the Facebook API, I am requesting permission for some information of the user by Javascript:

    FB.login(function (response) {
        if (response.authResponse) {
            // authorized
        } else {
            // canceled
        }
    }, {scope: 'email,user_birthday,user_location'});
    

    Now I want to get the user's information (when he is authorized) by PHP:

    $facebookUser = json_decode(file_get_contents('https://graph.facebook.com/' . $this->data['userid'] . '?access_token=' . $this->oathtoken . '&fields=id,name,location,gender,email,picture,birthday,link'));
    

    All working great, except that the API is not giving the user's birthday back. Even when I set my birthday privacy settings on public it won't show up.

    So:

    echo $facebookUser['birthday']; // Gives an error, since this key does not exists
    

    Does somebody have an idea how to fix this?

  • qaisjp
    qaisjp almost 12 years
    That doesn't answer his question though.
  • CBroe
    CBroe almost 12 years
    Well, it kinda does. Since he is not using the PHP SDK, but making a simple HTTP request on his own, no access token gets passed. So he either has to pass the token from client- to server-side by himself, or read it out of a cookie, and append it to the request URL himself – or use the SDK, which should handle all of these concerns on it’s own quite nicely.
  • Lumocra
    Lumocra almost 12 years
    I have tried that, but that key does not exists. So it seems I have not the permission to view it. Although I am requesting it...
  • Lumocra
    Lumocra almost 12 years
    I am saving information in a database, and didn't want to do an extra AJAX call. That's why I used it server side. I have tried your idea, but response.user_birthday is undefined when the callback is executed.