结合PHP和JS SDK Facebook身份验证。如何获得电子邮件

时间:2013-10-22 02:18:35

标签: php facebook facebook-graph-api facebook-javascript-sdk facebook-php-sdk

我知道之前已经问过这个问题,但我发现的所有答案似乎都已过时或与我使用的特定方法无关。此代码运行良好,但我无法弄清楚如何请求电子邮件地址权限。

<?php

require 'facebook/src/facebook.php';

$facebook = new Facebook(array(
  'appId'  => '3916913xxxxxx65',
  'secret' => 'xxxxxxxxxxxxx23dafe1d8c5867'
));

// See if there is a user from a cookie
$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) {
    echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
    $user = null;
  }
}

if ($user) {
    $fb_loggedin = true;
} else {
    $fb_loggedin = false;
    $fb_loginUrl = $facebook->getLoginUrl(array(
        'scope' => 'email,user_location',
        'redirect_uri' => 'http://'.$_SERVER['HTTP_HOST'].'/debut_'.($lang == 'fr' ? 'fr' : 'eng').'.php'
    ));
}

?>
<!DOCTYPE html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
  <body>
    <?php if ($user) {  

        echo $user_profile['first_name']; ?>
        <img src="http://graph.facebook.com/<?php echo $user_profile['username']; ?>/picture?type=large">
      Your user profile is
      <pre>
        <?php print htmlspecialchars(print_r($user_profile, true)) ?>
      </pre>
    <?php } else { ?>
      <fb:login-button></fb:login-button>
    <?php } ?>
    <div id="fb-root"></div>
    <script>
      window.fbAsyncInit = function() {
        FB.init({
          appId: '<?php echo $facebook->getAppID() ?>',
          cookie: true,
          xfbml: true,
          oauth: true
        });
        FB.Event.subscribe('auth.login', function(response) {
          window.location.reload();
        });
        FB.Event.subscribe('auth.logout', function(response) {
          window.location.reload();
        });
      };
      (function() {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol +
          '//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
      }());
    </script>
  </body>
</html>

0 个答案:

没有答案