您可以使用 Facebook 登录获取朋友的登录您网站的成员列表。但是,只会显示也在您网站上注册的朋友。 (在 Facebook 术语中,仅限使用相同应用的朋友)
您可以从 facebooks 开发者页面获取以下代码。
Login.php
$fb = new Facebook\Facebook([
'app_id' => '{app-id}', // Replace {app-id} with your app id
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.2',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email','user_friends']; // Optional permissions (THIS IS WHERE YOU GET THE PERMISSION FOR FRIEND'S LIST)
$loginUrl = $helper->getLoginUrl('https://example.com/fb-callback.php', $permissions);
echo '<a href="' . htmlspecialchars($loginUrl) . '">Log in with Facebook!</a>';
FB-callback.php
$fb = new Facebook\Facebook([
'app_id' => '{app-id}', // Replace {app-id} with your app id
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.2',
]);
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (! isset($accessToken)) {
if ($helper->getError()) {
header('HTTP/1.0 401 Unauthorized');
echo "Error: " . $helper->getError() . "\n";
echo "Error Code: " . $helper->getErrorCode() . "\n";
echo "Error Reason: " . $helper->getErrorReason() . "\n";
echo "Error Description: " . $helper->getErrorDescription() . "\n";
} else {
header('HTTP/1.0 400 Bad Request');
echo 'Bad request';
}
exit;
}
// Logged in
echo '<h3>Access Token</h3>';
var_dump($accessToken->getValue());
// The OAuth 2.0 client handler helps us manage access tokens
$oAuth2Client = $fb->getOAuth2Client();
// Get the access token metadata from /debug_token
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
echo '<h3>Metadata</h3>';
var_dump($tokenMetadata);
// Validation (these will throw FacebookSDKException's when they fail)
$tokenMetadata->validateAppId({app-id}); // Replace {app-id} with your app id
// If you know the user ID this access token belongs to, you can validate it here
//$tokenMetadata->validateUserId('123');
$tokenMetadata->validateExpiration();
if (! $accessToken->isLongLived()) {
// Exchanges a short-lived access token for a long-lived one
try {
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
} catch (Facebook\Exceptions\FacebookSDKException $e) {
echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n";
exit;
}
echo '<h3>Long-lived</h3>';
var_dump($accessToken->getValue());
}
$_SESSION['fb_access_token'] = (string) $accessToken;
// User is logged in with a long-lived access token.
// You can redirect them to a members-only page.
//header('Location: https://example.com/members.php');
在 login.php 你可以看到下面一行
$permissions = ['email','user_friends'];
这将提示用户授予访问也在使用该应用程序的朋友的权限。
此代码将从图中获取好友列表
try {
// Returns a `Facebook\FacebookResponse` object
$response = $this->fb->get('/me?fields=id,name,email,gender,user_friends,', $this->token);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
这些代码都是示例,仅供参考,以便您了解后端发生的情况。我强烈建议你看看https://developers.facebook.com/docs/php/howto/example_facebook_login
如果您对流程还有其他问题,您可以创建新问题以获得答案。
更新:处理 user_friends
使用 Facebook 登录您的网站后,登录的会员将获得您网站的永久唯一 ID。
当您请求该成员的 user_friends 图表时,Facebook Graph 将返回一个用户对象列表,供同时使用此应用的朋友使用。这些对象包括用户 ID(每个应用程序都是唯一的),通过该 ID,您将能够在您的应用程序中连接彼此认识的朋友。
https://developers.facebook.com/docs/graph-api/reference/user/
示例用户 A(ID:1234)和用户 B(ID:5678)
1) 用户 A 使用 Facebook 登录您的网站。
2) 您尝试从用户 A 的 Facebook 请求 user_friends 但它
什么都不返回。
3) 用户 B 使用 Facebook 注册并登录您的网站。
4) 您尝试请求用户 B 的 user_friends 和 Facebook 图表
返回用户 A 的 ID 1234。
从那里您可以了解 5679 和 1234 是朋友,并且您可以围绕它设计数据库以将您的成员关联为朋友。