Pages

How to Display Female Friends List only using Facebook Graph API

Monday 28 April 2014

How to Display Female Friends List only using Facebook Graph API





This tutorial is based on the Facebook Apps development. I have found there are very few tutorials on Facebook Apps development so i decided to give the series of Facebook Apps Tutorials here.

In this tutorial i am teach you that How to access  female friends only and display them using Facebook Graph API with PHP sdk......

The Code for accessing and displaying the Female friends only is shown below....

Learn How to Create Facebook App

Learn How to Get App ID and App Secret



<html>

 <body>
<?php

    $app_id = "YOUR APP ID";
    $app_secret = "YOUR APP SECRET";

    try
    {
        include_once ('libs/facebook.php');
    }
    catch(Exception $o)
    {
     print_r($o);
    }

    $fb = new Facebook(array(
     'appId'  => $app_id,
      'secret' => $app_secret,
      'cookie' => true
    ));

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

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

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

 if($friends['gender']=="famale")
 {
  $Mcount++;
  echo $friends['name']."<img src='https://graph.facebook.com/";.$friends['id']."/picture' width='50' height='50'  /><br/>";
 }
}

echo "Female Friends Count=".$Mcount;

?>
</body&gt
</html>

To get AppId and secret you have to create an App using your Facebook account...

No comments:

Post a Comment