The term social network has become so popular and familiar during the past couple of years that even non-techie people understand what the term means. Facebook, however, has its own vernacular when it comes to understanding social networking. They call it the social graph. The social graph can be defined as the interconnections that exist among family, friends, and acquaintances that every person has.

You do need to know a Web programming language such as PHP or Java.

Facebook has its own platform – called the Facebook Platform. The Facebook Platform is a standards-based programming framework that enables developers to create applications that interact and integrate with core Facebook services.

Now, Let’s try to understand the architecture of Facebook applications.

Facebook applications are not installed directly onto the Facebook server. Instead, they are placed on the developer’s server and then called by Facebook when the application URL is requested. To interact with applications, Facebook uses a callback metaphor. The URL of your application is associated with a registered application in Facebook. When the Facebook application URL (such as http://www.facebook.com/avengers) is requested, Facebook redirects the request to your server. Your application processes the request, communicates with Facebook using the Facebook Application Programming Interface (API) or Facebook Query Language (FQL), and returns Facebook Markup Language (FBML) to Facebook for presentation to the user inside of its canvas.

The Facebook Platform consists of four components that you work with

inside your code:

  • Facebook API
  • FBML (Facebook Markup Language)
  • FQL (Facebook Query Language)
  • Facebook JavaScript

Facebook API : The Facebook API is a Web services programming interface for accessing core services (profile, friends, group, event, photo) and performing other Facebook-centric functionality (log in, redirect, update view).

Facebook officially supports client libraries for PHP (4 and 5) and Java. These can be downloaded at developers.facebook.com/resources.php. Several unofficial client libraries have been developed for most every major Web programming language, including ASP.NET, ASP (VBScript), C++, C#, PHP4, Perl, Python, Ruby, VB.NET and Windows Mobile. To download these unofficial versions, go to wiki.developers.facebook.com.

When you use a client library, you use language-specific methods to access Facebook  services. For example, here’s how you can retrieve the name and picture of a user and then display it on a page:

$user_data = array(‘name’, ‘pic’, ‘activities’);

$current = $facebook->api_client->users_getInfo($user, $user_data);

echo “<p>Your name is {$current[0][‘name’]} </p>”;

echo “<p>Here is what you look like: <img src=’{$current[0][‘pic’]} /></p>”;

Facebook Markup Language (FBML) is an HTML-like language used to display pages inside of the Facebook canvas. Here are three basic facts about FBML:

  • FBML contains a subset of HTML elements. Such common elements as p, ul, and h1 are part of FBML.
  • FBML provides qualified support for script and style elements. Normal JavaScript is not supported in a script element, although Facebook JavaScript (see the “Facebook JavaScript” section below) does permit some scripting capabilities. You cannot use the style element to link to external style sheets, but you can use it for internal CSS.
  • FBML also includes several proprietary extensions for Facebook-specific UI elements as well as program flow.

For example, the fb:name element displays the name of the user based on

the value of the uid attribute:

<p>Your name is: <fb:name uid=”665127078” useyou=”false”/></p>

Consider the following:

<fb:if-can-see uid=”665127078” what=”profile”>

<p>You are granted access, you lucky one!</p>

<fb:else>Go somewhere else, you unfortunate one.</fb:else>

</fb:if-can-see>

Facebook Query Language (FQL) is a SQL-based interface into Facebook data. You can access many Facebook database tables, including user, friend, group, group_member, event, event_member, photo, album, and photo_tag. It is somewhat similar to SQL that you might be acquainted. You can actually perform most of the same data access routines using either API or FQL.

$friends = $facebook->api_client->fql_query(“SELECT uid, name FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=$user)”);

Facebook JavaScript (FBJS): FBML does not support the use of standard JavaScript using the script element. However, Facebook JavaScript (or FBJS, for short) allows for limited scripting functionality inside a Facebook application. Here are some of the differences between FBJS and JavaScript:

  • Instead of using the standard JavaScript DOM, Facebook provides its own alternative DOM implementation.
  • Many FBJS object implementations are the same as standard JavaScript, although there are some differences. For example, instead of accessing a JavaScript property (such as document.href), FBJS uses a pair of get and set methods instead (getHref, setHref).
  • When Facebook processes scripting code inside of script elements, it tacks on the application ID to function and variable names. Facebook does this to create an application-specific scope.

Before coding the application itself, you are going to need to get some application-specific information from Facebook to communicate with the Facebook Platform. Your first step is, therefore, to register the application with Facebook. When you do so, you’ll get an API key and secret key that you can plug into your code.

You can register from this site: www.facebook.com/developers

They provide with a API key and other details that you’ve to register before actual coding of your application. There is alternate way to register the server in which the app is to be loaded. This comes in with the Cloud platform and facebook provides a direct link to Heroku site for server upload. You can register for Heroku and they provide you with a different space where you can upload your app, in case you don’t have server of your own.

Note down the API key, the secret key for your app and the callback URL, for you’ll be needing them while coding of the app.

Furthermore, you can download the Client Library files from facebook.com/developers/apps.php on whichever language you’re planning to use.

Here’s a small demo on how to include the key files in your app. I’ve used PHP as a language here. However you can use java, or any other language to your desire.

<?php

require_once ‘../facebook-platform/client/facebook.php’;

// *** Add your Facebook API Key, Secret Key, and Callback URL here ***

$appapikey = ‘[your_api_key]’;

$appsecret = ‘[your_secret_key]’;

$appcallbackurl = ‘[your_web_app_url]’;

// Connect to Facebook, retrieve user

$facebook = new Facebook($appapikey, $appsecret);

$user = $facebook->require_login();

// Exception handler for invalid session_keys

try {

// If app is not added, then attempt to add

if (!$facebook->api_client->users_isAppAdded()) {

$facebook->redirect($facebook->get_add_url());

}

} catch (Exception $ex) {

// Clear out cookies for app and redirect user to a login prompt

$facebook->set_user(null, null);

$facebook->redirect($appcallbackurl);

}

?>

That’s all you need to get started. 🙂

Have fun building great apps for facebook 🙂 See you soon 😀