Facebook好友计数器

3

我想要做的是一个基于PHP的Facebook好友计数器,代码的结果应该类似于“你有1342个好友!”。

这是我正在使用的代码:

<?php 

require_once("../src/facebook.php");

    $config = array();
    $config[‘appId’] = 'MY_APP_ID';
    $config[‘secret’] = 'MY_APP_SECRET';
    $facebook = new Facebook($config);
    $user_id = "MY_USER_ID";

      //Get current access_token
    $token_response = file_get_contents
      ("https://graph.facebook.com/oauth/access_token?
      client_id=$config[‘appId’]
      &client_secret=$config[‘secret’]
      &grant_type=client_credentials"); 


  // known valid access token stored in $token_response
  $access_token = $token_response;

  $code = $_REQUEST["code"];

  // If we get a code, it means that we have re-authed the user 
  //and can get a valid access_token. 
  if (isset($code)) {
    $token_url="https://graph.facebook.com/oauth/access_token?client_id="
      . $app_id  
      . "&client_secret=" . $app_secret 
      . "&code=" . $code . "&display=popup";
    $response = file_get_contents($token_url);
    $params = null;
    parse_str($response, $params);
    $access_token = $params['access_token'];
  }


  // Query the graph - Friends Counter:
$data = file_get_contents
("https://graph.facebook.com/$user_id/friends?" . $access_token );
$friends_count = count($data['data']); 
echo "Friends: $friends_count";

echo "<p>$data</p>"; //to test file_get_contents

?>

因此,echo $Friends_count的结果始终为“1”。

然后我用echo测试了$data,并给出了所有朋友的列表,所以它获取了内容,但是没有正确计数...我该如何解决?

我已经尝试更改了

$friends_count = count($data['data']); 

对于

$friends_count = count($data['name']);

并且

$friends_count = count($data['id']);

但结果总是“1”。
上述代码的结果如下;
Friends: 1

{"data":[{"name":"Enny Pichardo","id":"65601304"},{"name":"Francisco Roa","id":"500350497"},etc...]

1
在计算之前,您需要对其进行json_decode()解码。 - Federico Quagliotto
http://stackoverflow.com/a/4066229/1100237 - groovekiller
2个回答

2

非常感谢!问题解决了!我之前不知道JSON对象,现在正在学习它...再次感谢!! - PhönixGeist

0

你可以轻松地通过fql获取好友数量。

用户表中有一个friend_count字段,您可以查询该字段。

SELECT friend_count FROM user WHERE uid = me();

https://graph.facebook.com/fql?q=SELECT friend_count FROM user WHERE uid={any id}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接