简体   繁体   中英

Get facebook user birthday with API

I'm trying to get user birthday with facebook API. I do know we need now to ask permission for this. I don't really know how it works

Just found this to add:

if ($user) {
    $logoutUrl = $facebook->getLogoutUrl();
} else {
    $loginUrl = $facebook->getLoginUrl(
     array('req_perms' => 'email,read_stream')
);
}

<fb:login-button autologoutlink="true" perms="email,user_birthday,status_update,publish_stream"></fb:login-button>

and this is my whole code ( quite basic ):

<?php
require 'php-sdk/src/facebook.php';
$facebook = new Facebook(array(
            'appId' => '4539768XXXXX',
            'secret' => 'fe86f3b0b34b3eXXXXXXXXXX',
            'cookie' => true,
        ));

$signedRequest = $facebook->getSignedRequest();
$liked = $signedRequest["page"]["liked"];


// Get User ID
$user = $facebook->getUser();



if ($user) {
    try {
        // Proceed knowing you have a logged in user who's authenticated.
        $user_profile = $facebook->api('/me');
    } catch (FacebookApiException $e) {
        error_log($e);
        $user = null;
    }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
    $logoutUrl = $facebook->getLogoutUrl();
} else {
    $loginUrl = $facebook->getLoginUrl(
     array('req_perms' => 'email,read_stream')
);
}
?>


<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
    <head>
        <style>
            body {
                font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
            }
            h1 a {
                text-decoration: none;
                color: #3b5998;
            }
            h1 a:hover {
                text-decoration: underline;
            }
        </style>

    </head>
    <body>


        <?php
        if ($liked) {
            header("Location:youlike.php");
        } else {
            echo "you don't like yet";
        }
        ?>


<fb:login-button autologoutlink="true" perms="email,user_birthday,status_update,publish_stream"></fb:login-button>

        <?php if ($user): ?>
            <a href="<?php echo $logoutUrl; ?>">Logout</a>
        <?php else: ?>
            <div>
                Login:
                <a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
            </div>
        <?php endif ?>

        <?php if ($user): ?>
            <h3>Vous</h3>
            <img src="https://graph.facebook.com/<?php echo $user; ?>/picture">

            <h3>Personne connectée</h3>
            <pre><?php print_r($user_profile); ?></pre>
        <?php else: ?>
            <strong><em>Vous n'etes pas loggé!</em></strong>
        <?php endif ?>


    </body>
    <script>
    </script>
</html>

i would appreciate your help thanks in advance!

You have a couple of problems in your code.

First, you aren't asking for the user_birthday permission when you create your $loginUrl . You should be requesting the same permissions for req_perms on line 33 as you do in the <fb:login-button> on line 68 of your code: email,user_birthday,status_update,publish_stream .

Once you copy over the permissions, you can remove line 68 of your code. The <fb:login-button> tag won't work without the JavaScript SDK.

Calling header() on line 61 after you've output text to the browser on lines 37-58 won't work. If you are going to redirect the browser, you can only do this before you've output text. Move this block of code before you output anything and it will work.

The white space between your ?> on line 36 and <!doctype> on line 39 will be output at the top of your HTML file. It cause the Facebook parser to choke. Some browsers don't like white space at the beginning of an HTML document either and will render your page incorrectly. Your code should really be ?><!doctype

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM