Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about Facebook and Php.

With more than 300 million active users and 50% of them using it every day (source), Facebook can be an interesting way to make some viral marketing.

One of the most effective ways is developing an application users (and users friends – and friends of users friends – and…) will use every day.

This tutorial will guide you through the creation of a simple application that will display how many male and female friends do you have.

Requirements

To develop a Facebook application with this tutorial, you will need:

* An active Facebook account
* An hosting plan supporting php
* Being my fan

ehm, actually being my fan is not strictly required, but it’s strongly recommended :)

Getting started

Go to developers area and click on Set Up New Application

Give a name to your application and agree terms.

Then, Facebook will create an API key and a secret key. Write them down (well, it’s not necessary since you will always find them in this page, but having something to write down makes me feel like I’m dealing with something important).

At this time the application is ready to run, but you can add more information and icons.

When done, go to Canvas page

Here, you will b asked for a Canvas Page URL that is the unique url of your application. Being obviously unique, all best names have been already taken (just like .com domains). In Canvas Callback URL, you have to enter the URL of the page on your server that will host the application. Then set Render Method to FBML

FBML mode lets you quickly start building an application from scratch, which is good for a absolute beginners. We’ll see it more in depth during next tutorials

At this time your application is ready to be executed.

Download the client library and copy the content of facebook-platform -> php to the Facebook application directory in your server – the same path of Canvas Callback URL.

Your directory now should look like this:

Now, t’s time to create the application itself:

require_login();

$friends = $facebook->api_client->friends_get();

echo "

Hello , you have ".count($friends)." friends"; foreach($friends as $friend){ $infos.=$friend.","; } $infos = substr($infos,0,strlen($infos)-1); $gender=$facebook->api_client->users_getInfo($infos,'sex'); $gender_array = array(); foreach($gender as $gendervalue){ $gender_array[$gendervalue[sex]]++; } $male = round($gender_array[male]*100/($gender_array[male]+$gender_array[female]),2); $female = 100-$male; echo "

  • Males: $male%
  • Females: $female%
"; ?>

Line 3: including Facebook library

Lines 5-6: Declaring variables with API key and secret code… yes, the two codes seen before

Line 8: Initializing a Facebook API

Line 10: Requiring the user to be logged into Facebook before using the application. If they are not logged in they will first be directed to a Facebook login page and then back to the application’s page. Then save the user unique id into $user_id variable

Line 12: Storing in the $friends array the unique id of all friends of yours

Line 14: This is the first FBML tag we see: Fb:name renders the name of the user specified by uid attribute. In this case, it will display your name. More information about Fb:name at this page. You can see all available tags at this page

Lines 16-20: Creating a string with all of your friends ids separated by comma, such as uid1,uid2,uid3,...,uidn

Line 22: users_getInfo returns a wide array of user-specific information for each user id passed. As you can see, I pass the whole list of friends and I only want to know their sex. More information about users_getInfo at this page

Line 24: Creating an array where I will store all genders

Line 26-31: Determining the % of each gender (male or female) in my friends list. You can see some additional math because sex value can be blank

Line 33: Displaying the results

Now that your application is completed, you can submit it to the Application directory. Before you are able to do it, your application must have at least 5 total users or 10 monthly active user.

Take a look at the “finished” application.

It should output something like that

Hello Kirenia, you have 49 friends
Males: 53.66%
Females: 46.34%

Warning: During the latest day there is a known bug called facebook auth_token loop that won’t allow you to run the application. If you experience problems, simply check back later.

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.