skip to Main Content

I am getting the facebook user’s number of friends. And I have no idea that how do I get the total_count.

Here is my code:

$helper = $fb->getRedirectLoginHelper();

try 
    if (isset($_SESSION['facebook_access_token'])) {
                $accessToken = $_SESSION['facebook_access_token'];

    } else {
        $accessToken = $helper->getAccessToken();
    }

  // Returns a `FacebookFacebookResponse` object
    $response = $fb->get('/me?fields=id,name,email,friends', $accessToken);

} catch(FacebookExceptionsFacebookResponseException $e) {
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(FacebookExceptionsFacebookSDKException $e) {
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}
echo $accessToken;
 $user = $response->getGraphUser();
 $userInfo = $user->asArray();

 print_r($userInfo);

However, the print return these:

( [id] => 1234567890 
  [name] => lmc_john
  [email] => [email protected] 
  [friends] => Array ( ) )

And on the Graph-api test tool, it returns these:

{
    "id": "12345667890",
    "name": "lmc_john",
     "friends": {
       "data": [
        ],
        "summary": {
        "total_count": 89
    }
  }
}

So, how can I only get the friend total_count from this array?

Thx for helping


Finally, I found another way to get the total friend.

Here is my source codde:
https://github.com/capslock10/Get_facebook_total_friend/blob/master/getSession.php

Hope can help someone.

3

Answers


  1. Simple, You can use method [count (array) = total_count]. refer enter link description here

    echo count($userInfo[‘friends’]);

    Login or Signup to reply.
  2. This is a guess, but maybe you can confirm whether its actually the case. friends is showing you the data, which is rightfully empty since your app does not have permission to see friends. However as described here, you can access the summary data for an edge.

    https://developers.facebook.com/docs/php/GraphEdge/5.0.0

    Some endpoints and edges of Graph support a summary of data. If the
    summary=true modifier was sent with a request on a supported endpoint
    or edge, Graph will return the total count of results in the meta data
    under $.summary.total_count. getTotalCount() will return that value or
    null if it does not exist.

    Login or Signup to reply.
  3. Ah, typical issue. So the easy way to solve this is to decode the graphbody and not to use GetGraphUser as GetGraphUser does not send over the total_count.

    Make sure to not use count() from “friends” as it only displays
    “Only friends who installed this app are returned in API v2.0 and higher. total_count in summary represents the total number of friends, including those who haven’t installed the app”

    Here’s the solution:

            $response = $response->getDecodedBody();
            $count = $response['friends']['summary']['total_count'];
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search