我正在努力计算用户的总朋友数并将其存储在数据库中。我有以下代码:
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类实例时使用taggable_friend
?我很困惑将插件插入到我的脚本中。我将非常感谢亲切的帮助。
答案 0 :(得分:0)
使用/me/friends
端点,并使用var_dump结果。您会在其中找到total_friends
,其中包含朋友总数。您可以在更改日志中详细了解它:https://developers.facebook.com/docs/apps/changelog#v2_1_new_features
答案 1 :(得分:0)
只需使用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,则应返回:
{
friends: {
data: [ ],
summary: {
total_count: 1187
}
},
id: "xxxxxxxxxxx"
}