Sunday, November 25, 2012

Facebook PHP Tutorials(Application)How to get Female/Male friends list separately using PHP for Facebook application

Facebook is a good platform to make funny Applications.We can use PHP,Javascript and Ruby and other languages make Facebook applications.In our case we use PHP , for making simple application PHP is best and easy.PHP is similar to C and C++ , almost all functions like while ,switch ,for ,if are also available in PHP. So we can make any application using PHP.Here we are going to give some tutorials to separate Girls (Female) and Boys ( Male) friend from user . We use multiple array for separating them.


 /* Friends data collection  start here*/

$user_friends=$facebook->api('/me/friends?list_type=family',array('fields' => 'id,name,gender,username,first_name','access_token'=>$accessToken));


//We create a array to store female friends name,id,username and gender.

$select_femaleid = array(
                                     'number'=>array(
                                                  "name" =>array(),
                                                       "id" =>array(),
                                            "username" =>array(),
                                                "gender"=>array()
                                                               )
                           );
$i=0;$female_count=0;





//We create array for male friends
  $select_maleid = array(
  'number'=>array(
                   "name" =>array(),
                        "id" =>array(),
             "username" =>array(),
                 "gender"=>array()
              )
                      );
$j=0;
$male_count=0;





//foreach property collect all data
//if and else if check gender and stores in a separate arrays


foreach ($user_friends as $friends) {
  foreach ($friends as $facebook_close) {
    if($facebook_close['gender']=='female'){
      $select_femaleid[$i]['id']=$facebook_close['id'];
      $select_femaleid[$i]['name']=$facebook_close['name'];
      $select_femaleid[$i]['username']=$facebook_close['username'];
      $select_femaleid[$i]['first_name']=$facebook_close['first_name'];
      $i++;
      $female_count++;
 
  }

else if($facebook_close['gender']=='male'){
          $select_maleid[$i]['id']=$facebook_close['id'];
      $select_maleid[$i]['name']=$facebook_close['name'];
      $select_maleid[$i]['username']=$facebook_close['username'];
      $select_maleid[$i]['first_name']=$facebook_close['first_name'];
      $i++;
      $male_count++;
  }


}

}

//  rand  is used to select random numbers.For example rand(1,10) will any number range from 1-10 it may be 3 or 5,9 or any number betweebn1 and 10.$female_count count all female friends and $female_rand this variable store numberof female friends.

$female_rand=rand(0,$female_count-1);
$male_rand=rand(0,$male_count-1);
//End of codes

//Starting of storing images and other process 

$friend_image = file_get_contents('https://graph.facebook.com/'.$select_femaleid[$female_rand]['username'].'/picture?type=large'); // sets $image to the contents of the url
                             $file = dirname(__file__). "/user_friend/" . $select_femaleid[$female_rand]['id']. ".jpg";
                            file_put_contents($file, $friend_image);

//$select_femaleid[number][data] -=It is a multidimentional array.In C language we use two pointer to point out a data.
for example  in c 

table [2][3]={{1,2,3},{4,5,6}}

it become  1  2  3
                4  5  6
table[0][1] will gibe 0th row and 1th column , answer is 2 
same way we use our PHP code first it select number row and then to data Female/Male number is stored in number array , first it select any number ( one of friends ) then to data.Same way we can access all details like name,usernmae,gender etc...

Get 10-50% offer on all purchace - Flipkart offer zone

2 comments:

MM Enterprises said...

This is very deep technic, good info.

Deepak said...

i was founding this type of script and finally i got it in your site.
thanks for this post.

 

Copyright @ 2013 7CHIP.

Designed by Templateify & Sponsored By Twigplay