使用PHP获取FB好友列表

时间:2011-08-08 05:08:48

标签: php facebook

我想知道如何使用php获取FB好友列表。我该如何为此编写代码?

1 个答案:

答案 0 :(得分:4)

您应该通过下载Facebook SDK并查看example.php来找到所需内容。以下显示了如何通过图形API完成此操作的一种方法:

// Create our Application instance.
// Get your application id and secret from Facebook to fill these in
$facebook = new Facebook(array(
  'appId' => '12345...',
  'secret' => 'a1b2c3...',
  'cookie' => true,
));

//print_r($_REQUEST);

$fbUser = $facebook->getUser();

if ($fbUser) { 
  try { 
  $fbUserProfile = $facebook->api('/me'); // Get User
  $friends = $facebook->api('/me/friends'); // Get Friends

  // Bonus: Get info from Coca-colas page. Requires no access token as public.
  // $cocacola = $facebook->api('/cocacola'); 

  } catch (FacebookApiException $e) { 
  error_log($e); 
  $fbUser = null; 
  } 
} else {
   // There's no active session, let's generate one
    $login_url = $facebook->getLoginUrl();
    // Extra permissions can be requested as follows:
    // $login_url = $facebook->getLoginUrl(array('req_perms' => 'email, user_location')); 
    header("Location: ".$login_url);  
}

echo "<pre>";
print_r($fbUserProfile);
echo "</pre>";

echo "<pre>";
print_r($friends);
echo "</pre>";

echo("Hello " . $testuser->name);
echo("<img src='https://graph.facebook.com/{$fbUserProfile->id}/picture?type=large'>");