È possibile ottenere l'elenco di amici di membri che accedono al proprio sito Web utilizzando l'accesso di Facebook. TUTTAVIA, verranno visualizzati solo gli amici che sono anche registrati sul tuo sito web. (In termini di Facebook, solo gli amici che utilizzano la stessa app)
Dalla pagina degli sviluppatori di facebook è possibile ottenere i seguenti codici.
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');
A login.php si può vedere la seguente riga
$permissions = ['email','user_friends'];
Questo richiederà all'utente l'autorizzazione per accedere ai tuoi amici che sono anche utilizzando l'applicazione.
Questo codice otterrà la lista amico dal grafico
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;
}
I codici sono tutti campioni e sono solo di riferimento in modo da ottenere l'idea di ciò che sta accadendo nel backend. Consiglio vivamente di dare un'occhiata a https://developers.facebook.com/docs/php/howto/example_facebook_login
In caso di ulteriori domande sul flusso, è possibile creare una nuova domanda per ottenere le risposte.
Aggiornamento: Manipolazione user_friends < - questo è quello che stai cercando per
Su login utilizzando Facebook al tuo sito web, l'utente loggato sarà dato un ID univoco permanente al tuo sito web.
Quando si richiede il profilo user_friends di tale membro, Facebook Graph restituirà un elenco di oggetti utente per gli amici che utilizzano questa app. E quegli oggetti includono l'id utente (che è unico per ogni app), dall'ID sarai in grado di collegare amici che si conoscono all'interno della tua app.
https://developers.facebook.com/docs/graph-api/reference/user/
Esempio Utente A (ID: 1234) e l'utente B (ID: 5678)
1) L'utente A login al tuo sito web utilizzando Facebook.
2) Si tenta di richiedere user_friends da Facebook dell'utente A, ma non restituisce nulla. .
3) Utente B registrati e accedi al tuo sito web utilizzando Facebook.
4) È una richiesta user_friends di utente B e grafici Facebook riporta l'ID utente di un 1234.
Da lì si è in grado di comprendere 5679 e 1234 sono amici e si è in grado di progettare il database intorno a questo per associare i tuoi membri come amici.
È possibile eseguire questa operazione solo se si memorizzano gli ID utente fb dei propri utenti (ad esempio, gli utenti sul proprio sito possono collegare i propri account ai profili Facebook e memorizzare tali informazioni). – serg
Che dire dell'utilizzo di alcune informazioni identificative fornite da Facebook come l'e-mail? C'è un modo per abbinare le informazioni di un utente sul mio sito alle informazioni su Facebook? –