Why write apps for facebook
Have you ever thought that the most popular applications on Facebook are simple and similar in essence? All these virtual hugs, greeting cards, wishes, sympathies, kicks, smiles, karma exchange and other psychological “strokes” differ only in the form in which the user receives his next batch of attention. Thus, this niche is always open for those who want to try out their own idea. Especially now, when Facebook started talking in Russian, and Russian-language applications are especially in demand. Facebook applications are gaining audience in the manner of "word of mouth", like a chain reaction. If you have a really good idea, then by implementing it, you get a potential audience of 20 million Facebook users. With such a scale, it is not surprising that some amateur Facebook applications are sold for tens of thousands of dollars.
However, skeptics can rightly say here: “If it's so easy to make money on Facebook, then why aren't we still rich on it ?!”. It's like in a lottery - everyone can try, but luck will smile on just a few. On the other hand, you can offer any application to your Facebook audience to your taste. I recently came across a simple compatibility questionnaire for those who love to travel. Do something like this and place on the application pages the tour operator logo for the agreed amount. However, why fantasize? Ways to monetize a successful application are
described on Facebook . In addition, having behind a couple of popular applications, you can declare yourself on the
Facebook Marketplace and get the desired orders. Or, you can
search for a customer yourself
on applications for Facebook . Offers like “$ 50,000 - Super Developer Needed” are usually the case on this bulletin board.
What is required in order to write an application for Facebook
First of all, you need a user account on Facebook. If you still do not have an account there - start it. It takes you no more than a few minutes. Next, you need your own server for hosting scripts and other application files. As a programming language, you can use any suitable for creating web applications. Currently, applications written in ASP.NET, ASP (VBScript), ColdFusion, C, C #, D, Emacs Lisp, Java, JavaScript, Lisp, Perl, PHP, Python, Ruby on Rails, VB.NET are in the Facebook directory. . For PHP and Java languages, Facebook provides client libraries. The application database should also be created on its own server.
If you have all this, you will only have to learn the FBML markup language, the FBJS scripting language, the FQL query language and the Facebook API ... In fact, everything is not as scary as it seems. Later we will look at these languages ​​and talk about how to use them.
How applications for Facebook are arranged
Before you start designing a new application for Facebook, you should
study the anatomy of applications for this platform . In short, you are provided with a workspace (Facebook Canvas Pages) of the application and a link to it in the left navigation pane (Left Nav).
')

The activity of the user's friends within this application can be displayed on the user's home page. To do this, the application logs the activity of users by placing records in the New Feed.

The application in abbreviated form can be represented in a special block (Profile Box) on the user profile page. An application can add a link to an action to a user’s profile. For example, if your application implies users' mutual praise, it will be logical to add a “Praise User” link to the application subscribers profile.
The application can send notifications to Email (Alert), as well as send users invitations to take part in any event. Let's say your application invites all participants to virtually join hands in support of Tibet. You can invite app users to send an invitation to this event to random Facebook users.
Where to begin
Go to the page
http://www.facebook.com/developers and click on Add Developer to add the Developer application.

Now that this application has appeared in the left navigation pane, we can open it. There will be a link to the client library and an example application. Now we press the “Set Up New Application” button and get to the “New Application” page. Fill out the form. In the "Application Name" enter a short, but intriguing name. In the field “Callback Url” we indicate the address of the application on our remote server. In “Canvas Page URL” we tell you what address you want on Facebook. Set the checkbox “Can your application be added on Facebook?” To the “Yes” position. This will open the “Installation Options” section. In her answer to the question “Who can add your application to their Facebook account?” We answer “All pages” and put a tick in front of “Users”. In the "Post-Add URL" specify the expected address on Facebook (
http://apps.facebook.com/my_cool_app/ ). We are not lazy to describe adequately the application in the "Application Description". In the "Integration Points" section, in the "Side Nav URL" field, specify the address on Facebook again to get the link to the application in the left navigation bar. Click "Submit" and get the application home page, where API Key and Secret will be presented. We will need them in the future.

How to program an application
As I said, to create an application you should familiarize yourself with at least FBML and FBJS languages. However, for serious tasks you can not do without FQL and Facebook API as well. As you probably guessed, each time you access your Facebook app page, it will take the page code from your site, then convert it in its own way and display it to the user. If he meets the design language FBML, he follows them. Those. you do not need to write the code of your pages in a new language unknown to you. You are free to use (x) HTML, but extend it with FBML. In this regard, FBML reminds me, so close to the heart of
XML Sapiens . For example, you can get the correct link to the user profile and its name using the following construction:

In the
FBML language reference book you will find many tools for obtaining information about users, groups and their status, profile view, using various media formats, displaying ready-made widgets for commenting, sending requests to a selected group, data entry forms, dialog boxes, captcha and much another. All in the style of Facebook, as you know. FBML also serves logical expressions (fb: if / fb: else, fb: switch, etc.). You can see how this happens in
the sandbox of Facebook . In many cases, using FBML is quite convenient, which can hardly be said about FBJS. All Java Script inclusions on the pages of your application will also be converted.
For example, the function:
function getWinSize () {
w = document.documentElement.clientWidth;
h = document.documentElement.clientHeight;
return {width: w, height: h};
}
Turns into something like:
function a12345_getWinSize () {
a12345_w = a12345_document.documentElement.clientWidth;
a12345_h = a12345_document.documentElement.clientHeight;
return {width: a12345_w, height: a12345_h};
}
Here we are. You can part with your vain dreams about using your personal JS libraries or your favorite frameworks in a Facebook application. We'll have to write all the required functions in a new way, while checking every step with the
documentation . On the other hand, Facebook provides a simple framework that allows you to
serve asynchronous controller requests (AJAX) and create
dialog boxes .
FBML and FBJS allow you to build a simple application. However, if you need, say, a sample of Facebook users according to their interests, ready-made FBML widgets can hardly help you. In this case, there is
a Facebook REST server serving a wide range of remote procedures. All of them are described in the
documentation , and for the test of strength there is a
sandbox . But more than that, you can get samples directly from Facebook databases through queries that are very similar to the SQL we are used to. All
tables and fields are carefully described . There are
various examples of queries . All that is required is to form a request, send it in the parameters of the facebook.fql.query REST call and parse the server response.
Creating an application in practice
How your software will be organized does not really matter, at least for Facebook. You just have to give (x) HTML inside the BODY tag when requesting application pages. At the same time, you should initiate a class loaded from the client library Facebook:
require_once 'vendors / facebook / facebook.php';
$ appapikey = 'your API Key;
$ appsecret = 'your secret';
$ facebook = new Facebook ($ appapikey, $ appsecret);
$ user_id = $ facebook-> require_login ();
The only thing I can add here is to use the Facebook user interface where appropriate. For example, the main menu of the application can be placed in tabs:
<fb: tabs>
<fb: tab-item href = "
apps.facebook.com/study_english " title = "Quizze" selected = "true" />
<fb: tab-item href = "
apps.facebook.com/study_english/?page=course " title = "Course" />
<fb: tab-item href = "
apps.facebook.com/study_english/?page=rating " title = "TOP 50 Users" />
<fb: tab-item href = "
apps.facebook.com/study_english/?page=invite " title = "Invite friends" />
</ fb: tabs>
For the form of inviting friends to use your application, you can use a ready-made widget:
<fb: fbml>
<fb: request-form action = "" method = "POST" invite = "true" type = "new cute app" the language is phrasal verbs. Do you know them enough? Test yourself here.
<? print htmlentities ("<fb: req-choice url ="
apps.facebook.com/study_english "label =" Add My APP! "/>");?> ">
<fb: multi-friend-selector showborder = "false" actiontext = "Invite Up Your English.">
</ fb: request-form>
</ fb: fbml>
But regarding FBSJ, my experience can save you time. An example of
using AJAX provided by Facebook is unlikely to suit you. It is too simplified. I made on its base a more versatile option.
Java scriptfunction $ (divName) {return document.getElementById (divName); }
function user_event (div_id) {
callRemoteProc (
{
"Ctrl_action": "user_event",
"Param1": "param2 data",
"Param2": "param3 data"
}, div_id);
return false;
}
function callRemoteProc (params, bind_id) {
var ajax = new Ajax ();
ajax.responseType = Ajax.JSON;
ajax.ondone = function (data) {
if (data.ErrorMsg) {
// Shows the error message
new Dialog (). showMessage ('Error', data.ErrorMsg);
} else {
if (data.ActionCode == 1) {
// Example:
// In the case of a request for action type 1
// put the data in the given DIV
$ ("Ex" bind_id) .setInnerXHTML (data.Body);
}
}
}
ajax.requireLogin = 1;
ajax.post ('http: // my_server/facebook.ctrl.php'params);
}
The callRemoteProc () function serves requests to the controller and its responses. The ajax.ondone = function (data) {} section describes how we proceed with the controller's response. Through the ActionCode attribute, we specify in the controller response how exactly the response should be serviced. If the ErrorMsg attribute is set, the function displays an error message. The Body attribute contains a response code, for example (x) HTML. In this case, the Body content is placed in the DIV with the given identifier.
You can assign functions similar to user_event () to various events in the application to make a request to the controller. For example, in my application “
Brush Up Your English ” displays a list of sentences that are proposed for filling by means of phrasal verbs. When the user clicks on the answer check button, a request is sent to the controller.
Facebook.ctrl.php controller<?
// ...
include ("libs / controller.lib.php");
class RD extends controller {
function user_event () {
if (! isset ($ _ REQUEST ["param1"])) {$ this-> ErrorMsg = "Param1 is not defined"; return false; }
// ...
$ this-> ActionCode = 1;
$ this-> Body = 'Correct sentence';
return false;
}
}
$ rd = new RD ();
if (isset ($ _ REQUEST ["ctrl_action"])) {
$ rd -> $ _ REQUEST ["ctrl_action"] ();
}
$ rd-> respond ();
?>
Controller.inc.php library<?
class controller {
var $ ActionCode = 1;
var $ ErrorMsg = "";
var $ Body = "";
function respond ($ message = "", $ errormsg = "") {
if ($ message) $ this-> Body = $ message;
if ($ errormsg) $ this-> ErrorMsg = $ errormsg;
$ out = '{
“ActionCode”: "'. $ This-> ActionCode.'",
"ErrorMsg": "'. ($ This-> ErrorMsg? Addslashes (preg_replace (" / [rn] / "," ", $ this-> ErrorMsg)):" ").'",
"Body": "'. ($ This-> Body? Addslashes (preg_replace (" / [rn] / "," ", $ this-> Body)):" ").'"
} ';
header ("Content-type: text / html; charset = UTF-8");
print $ out;
exit;
}
}
?>
In the controller, we refer to the procedure requested in the ctrl_action parameter when calling the callRemoteProc () function from JS. In this case, it is user_event (), in the course of which the attributes ErrorMsg, ActionCode, Body are assigned. Notice that the body’s response is in the span tags. This is done because we have to use the setInnerXHTML () method on the FBJS side, which requires XML matching for the input parameters.
Now with more you have everything in order to try your hand at creating applications for Facebook. Is it possible that the
list of URLs for various actions on the user can still be useful (see the profile, “kick” the user, send him a letter, etc.).
The application is ready. What's next?
As you understand, users themselves will not come to your application. They simply will not know about its existence. You say, “What about the Facebook application directory?”. So, in order to get into this directory you will need to acquire at least five users. However, the last position in the catalog, behind 20 thousand other applications is also not a way to gain popularity.
You can search for Facebook users who may be interested in your application, by interest or in groups. Invite them to become friends and send them a request to install the application. If they like the application, they will send a request to their friends. If you are ready to invest certain funds in advertising, go to the
Developer application page and click the “Advertise” link in the “more” drop-down list opposite your application.
Original article at
http://blog.cmsdevelopment.com