用API获取facebook用户生日

0 php api facebook

我正试图用facebook API获得用户生日.我知道我们现在需要获得许可.我真的不知道它是如何工作的

刚刚发现这个添加:

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>
Run Code Online (Sandbox Code Playgroud)

这是我的整个代码(非常基本):

<?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>
Run Code Online (Sandbox Code Playgroud)

我将非常感谢您的帮助!

cpi*_*lko 5

您的代码中存在一些问题.

首先,您user_birthday在创建时不会要求获得许可$loginUrl.您应该req_perms在第33行请求与<fb:login-button>代码的第68 行相同的权限:email,user_birthday,status_update,publish_stream.

复制权限后,您可以删除代码的第68行.<fb:login-button>没有JavaScript SDK ,标签将无法使用.

header()在第37-58行输出文本到浏览器后调用第61行将不起作用.如果要重定向浏览器,则只能在输出文本之前执行此操作.在输出任何内容之前移动此代码块,它将起作用.

?>36行和<!doctype>第39 行之间的空白区域将输出到HTML文件的顶部.它导致Facebook解析器窒息.有些浏览器不喜欢HTML文档开头的空格,也会错误地呈现页面.你的代码应该是真的?><!doctype