且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在PHP中从Facebook获取用户标识

更新时间:2023-02-12 21:50:38

下载 PHP SDK

一个非常简单的代码示例来获取用户ID - 如果用户已经登录并且已经授权了应用程序,那么 $ facebook-> getUser() 将给你的用户ID:

A very simple code example to get the user id - if the user is logged in and has authorised the application then $facebook->getUser() will give you the users id:

require 'facebook.php';

$facebook = new Facebook(array(
  'appId'  => 'YOUR_APP_ID',
  'secret' => 'YOUR_APP_SECRET',
));

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

if ($user) {
  try {
    // Get the user profile data you have permission to view
    $user_profile = $facebook->api('/me');
    echo "<pre>";
    print_r($user_profile);
    echo "</pre>";
  } catch (FacebookApiException $e) {
    $user = null;
  }
} else {
  die('<script>top.location.href="'.$facebook->getLoginUrl().'";</script>');
}

查看SDK中的示例和 Facebook开发者网站

Take a look a the examples within the SDK and on the Facebook Developers Site.