如何使用PHP获取登录用户在facebook中的朋友列表?

时间:2013-12-24 09:49:20

标签: php facebook facebook-graph-api facebook-php-sdk

我使用以下代码获取给定密钥的朋友列表,如何获取登录用户ID的朋友列表(无论appid和秘密ID)。

 <?php

   $facebook = new Facebook(array(
       'appId' => 'appId',
       'secret' => 'secretKey',
       'cookie' => true,
           ));

    $app_id = "appId";
    $app_secret = "secretKey";
    $my_url = "myurl";


    try {
        $uid = $facebook->getUser();
        $me = $facebook->api('/me');
        $friends = $facebook->api('/me/friends');
      } catch (FacebookApiException $e) {
       error_log($e);
     }
    ?>

3 个答案:

答案 0 :(得分:0)

要从图形API中获取数据,您需要与应用程序关联(即您必须使用应用程序ID和应用程序密钥)。

答案 1 :(得分:0)

尝试:

<?php

    $app_id = "appId";
    $app_secret = "secretKey";
    $my_url = "myurl";

   $facebook = new Facebook(array(
       'appId' => $app_id ,
       'secret' => $app_secret ,
       'cookie' => true,
           ));

答案 2 :(得分:0)

这是完整的经过测试的代码

<?php
require '/src/facebook.php';
$facebook = new Facebook(array(
  'appId'  => '738188999529253',
  'secret' => '744a01c3630802882850754898bebb32',
));

$app_id =   '738188999529253';
$app_secret = '744a01c3630802882850754898bebb32';

$user = $facebook->getUser();

if ($user) {
  try {
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}
if ($user) {
    $logoutUrl = $facebook->getLogoutUrl();

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

    print "<pre>";
    print_r($result);
    print "</pre>";


} else {
  $statusUrl = $facebook->getLoginStatusUrl();
  $loginUrl = $facebook->getLoginUrl(array('scope' => 'user_friends,read_stream, export_stream'));
}


?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
  <head>
    <title>demo</title>
  </head>
  <body>
    <?php if ($user): ?>
      <h3>You</h3>
      <img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
      <a href="<?php echo $logoutUrl; ?>">Logout</a>
    <?php else: ?>
      <div>
        Login:<a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
      </div>
    <?php endif ?>                                
    </body>
</html>