Can';我不知道如何计算facebook php sdk中的好友总数
我正在努力计算一个用户的好友总数并将其存储在数据库中。我有以下代码:Can';我不知道如何计算facebook php sdk中的好友总数,facebook,facebook-graph-api,facebook-php-sdk,Facebook,Facebook Graph Api,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\Face
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\u count
的变量中,以便将总数保存在数据库中
在这里搜索我得到了这个
$userfriends = $facebook->api('/XXXXXXX152466/friends');
$friend_count = COUNT($userfriends['data']) + 1;
所以,如果我在
api()
中传递$fbid变量,它会计算朋友总数吗?或者我应该在创建FacebookRequest类实例时使用taggable\u friend
?我很困惑把这个插入我的剧本。我们将非常感激您的慷慨帮助 使用/me/friends
端点,然后var\u转储结果。您将在其中找到total_friends
,以及好友总数。您可以在变更日志中阅读更多信息:只需使用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"
}
@感谢您的回复。所以我必须使用$request=newfacebookrequest($session,'GET','me/friends');但是如果我通过了friends,grahpObject还会返回其他数据吗?身份证、电子邮件、全名?或者我应该提及我想要检索的所有数据吗
me?fields=姓名、性别、朋友
这样?您想要的是朋友的数量,而不是他们的数据。这就是我从你的问题中读到的。正如我所说,只需使用没有任何参数的/me/friends,结果中就会有一个计数。只需调试id。请不要问如何获取所有好友数据,因为在成百上千的stackoverflow问题中已经解释过,您只能获取授权您应用的好友的数据;)-你只能得到总数。