无法弄清楚如何计算 facebook php sdk 中的好友总数

Can't figure out how to count total friends in facebook php sdk

我正在努力计算用户的好友总数并将其存储在数据库中。我有以下代码:

session_start();
// added in v4.0.0
require_once 'autoload.php';
require_once 'functions.php';

use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\Entities\AccessToken;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookHttpable;
// init app with app id and secret
FacebookSession::setDefaultApplication( 'ap id','secret key' );
// login helper with redirect_uri
    $helper = new FacebookRedirectLoginHelper('http://mykadamtala.com/facebook/fbconfig.php' );

try {
  $session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
  // When Facebook returns an error
} catch( Exception $ex ) {
  // When validation fails or other local issues
}
// see if we have a session
if ( isset( $session ) ) {
  // graph api request for user data
  $request = new FacebookRequest( $session, 'GET', '/me' );
  $response = $request->execute();
  // get response
  $graphObject = $response->getGraphObject();
 $fbid = $graphObject->getProperty('id');              // To Get Facebook ID
        $fbfullname = $graphObject->getProperty('name');
        $firstname = $graphObject->getProperty('first_name');
        $lastname = $graphObject->getProperty('last_name');
        $gender = $graphObject->getProperty('gender');
        $femail = $graphObject->getProperty('email');
       $friend_count = ????//how i can get total friends counts of the signed up users?

    /* ---- Session Variables -----*/
        $_SESSION['FBID'] = $fbid;  
        $_SESSION['FULLNAME'] = $fbfullname;
        $_SESSION['EMAIL'] =  $femail;

      //storing values to the database. Here i want to store the friend count
        checkuser($fbid,$firstname, $lastname, $femail, $check, $friend_count); 
    /* ---- header location after session ----*/
  header("Location: index.php");
} else {
  $loginUrl = $helper->getLoginUrl(array(
   'scope' => 'email'
  ));
  header("Location: ".$loginUrl);
}

我想将用户的朋友总数存储在一个名为 $friend_count 的变量中,这样我就可以将总数保存在我的数据库中。

在这里搜索我得到这个

$userfriends = $facebook->api('/XXXXXXX152466/friends');
$friend_count =  COUNT($userfriends['data']) + 1;

那么,如果我在 api() 中传递 $fbid 变量,它会计算好友总数吗?或者我应该在创建 FacebookRequest class 实例时使用 taggable_friend 吗?我很困惑将其插入我的脚本。亲切的帮助将不胜感激。

使用 /me/friends 端点,var_dump 结果。你会发现里面有total_friends,里面有好友总数。您可以在变更日志中阅读更多相关信息:https://developers.facebook.com/docs/apps/changelog#v2_1_new_features

只需使用getDecodedBody方法即可。

像那样:

// Get basic info on the user from Facebook.
try {
    $response = Facebook::get('/me?fields=friends');
} catch (Facebook\Exceptions\FacebookSDKException $e) {
    dd($e->getMessage());
}

return $response->getDecodedBody();

如果将响应转换为 Json,应该 return:

{
    friends: {
        data: [ ],
        summary: {
            total_count: 1187
        }
    },
    id: "xxxxxxxxxxx"
}