Facebook好友列表

时间:2011-08-07 05:08:33

标签: facebook facebook-graph-api

<?php

require_once 'facebook/facebook.php';

$appapikey = 'xx';
$appsecret = 'yy';
$facebook = new Facebook($appapikey, $appsecret);
$user_id = $facebook->require_login();

echo "<p>Hello, <fb:name uid=\"$user_id\" useyou=\"false\" />!</p>";

echo "<p>Friends:";
$friends = $facebook->api_client->friends_get();
$friends = array_slice($friends, 0, 15);
foreach ($friends as $friend) {
echo "<br><fb:profile-pic uid='$friend'><fb:name uid=\"$friend\" useyou=\"false\" /></fb:profile-pic>";
}
echo "</p>";

?>

我该怎么办呢?我不知道新的代码。请等待你的帮助:(

2 个答案:

答案 0 :(得分:2)

您需要使用PHP SDK for the Facebook API

require 'php-sdk/src/facebook.php';

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

获取朋友列表:

$friendsLists = $facebook->api('/me/friends');

foreach ($friendsLists as $friends) {
  foreach ($friends as $friend) {
    $id = $friend['id'];
    $name = $friend['name'];
  }
}

答案 1 :(得分:2)

我将Gaurav的上述答案扩展到包括限制器和洗牌器。

$i = 0;

foreach ($friendsLists as $friends) {
    shuffle($friends);
    foreach ($friends as $friend) {
        $id = $friend['id'];
        $name = $friend['name'];
        echo $name;
        echo "<img src='http://graph.facebook.com/" . $id ."/picture'><br>";
        if (++$i == 5) break;
    }
}