Pages

How to Count All Your Facebook Friends using Facebook Garph API with PHP SDK

Monday, 28 April 2014

How to Count All Your Facebook Friends using Facebook Garph API with PHP SDK





How to Count All Your Facebook Friends using Facebook Garph API with PHP SDK?

Hello and welcome on the Facebook Graph API with PHP SDK Tutorial blog, On this blog i will teach you how to access the facebook app user's info in your custom app with Facebook Graph API using PHP SDK like User's Name, Profile Pic, Latest Post, Likes etc.

In this tutorial i will tell you how to count all friends and display the final count on your app, here is a simple code to access all friends and then count them and display the final count...

The below example is a complete code to access your all friends and count them...


 $app_id,
      'secret' => $app_secret,
      'cookie' => true
    ));

    $user = $fb->getUser();
    $loginUrl = $fb->getLoginUrl(array
      (
                'scope' => 'email'
            )
        );

    if ($user)
    {
       try
       {
        $fbuser=$fb->api('/me/friends');
         $access_token = $fb->getAccessToken();
       } catch (FacebookApiException $e)
       {
         echo $e;
         $user = null;
       }
    }

    if (!$user) {
        echo "<script type="text/javascript">top.location.href = '$loginUrl';</script>";
        exit;
    }
    $friend_count=0;
    foreach($fbuser['data'] as $friends)
    {
  $friends_count++;

 }

 echo "You have Total ".$friend_count." Friends on Facebook";

?>

Now copy the above code and paste it to your app and get the all friends count in your app...

No comments:

Post a Comment