Creating Facebook Application Tech Paper
Creating Facebook Application Tech Paper
José León
November 2010
Table of Contents
Introduction ....................................................................................................................................................- 2 -
Conclusion ...................................................................................................................................................- 15 -
INTRODUCTION
Facebook, the leading social networking site, is increasing being used as a platform for
applications of all types - including games, marketing promotions, business applications
and utilities. Building applications to run on Facebook opens opportunities for developers
to reach an audience of more than 500 million users. According to Facebook, every month
more than 70% of Facebook users engage with platform applications. If you haven’t tried
developing applications for Facebook yet, the process can be easier than you think.
The goal of this technical paper is to show how to develop a simple Facebook application
using Embarcadero RadPHP™ XE. We will create a utility application that tracks lending
items to Facebook friends. The paper covers everything from the setup of the application
in the Facebook network to creating the application and gathering information from users
and friends.
Here it’s a very simplified flow diagram on how all this works:
The FBApplication component allows you to connect with Facebook and use its API. It also
provides the ApplicationID and ApplicationSecret properties to uniquely identify your
application into the Facebook network.
http://www.facebook.com/developers
You will be asked for an Application Name and a captcha verification, your application will
be created, and you will be forwarded to editing the application parameters. There are
five categories for settings (About, Web Site, Facebook Integration, Mobile and Devices,
Advanced), but for this sample, we are interested only on the Facebook Integration
settings. From top to bottom, the interesting settings are:
• Application ID
• Application Secret
• Canvas Page
• Canvas URL
• Canvas Type
• IFrame Size
The easiest way to do this is to compress the RPCL folder in a .tar.gz file, and upload it to
the server. After uploading, access the server using SSH and decompress this file. Finally,
delete the .tar.gz file and you are done.
DATABASE DESIGN
For this sample, we need a simple table in a database. We want to store the ID of the user
lending out items, the IDs of the user receiving items, and a description of what the items
(“stuff”) are.
The Database component provides the connectivity with the database itself. You need to
setup the properties DatabaseName, DriverName, Host, UserName and UserPassword
with the correct values.
The Table component should be linked to the Database component through the
Database property, then set the TableName property to the name of the table we want to
access, in this case, “lendstuff”.
We need to filter the Table component so it only retrieves records for the current logged
users. In the OnBeforeOpen event of the Table, we can require the user to be logged in,
in order to get the User ID.
We are storing the User ID in a DataModule property, so we can reuse it later if required.
This is the code for such property:
1. protected $_loggeduserid='';
2.
3. function readLoggedUserID() { return $this->_loggeduserid; }
4. function writeLoggedUserID($value) { $this->_loggeduserid=$value; }
5. function defaultLoggedUserID() { return ''; }
First, let’s place a Label component at the top, specifying the purpose of the application
(in this case, MyLendStuff – Remember what you lend to others). After that, let’s place
another Label with the current page contents (Currently lent stuff), to show a list of the
current stuff you have lent.
To show the stuff, we need to iterate through a database table and show the profile image
for the friend who borrowed the stuff from us, the name for it, and the stuff description.
For that, we are going to use a DBRepeater component. This component is attached to a
Datasource and iterates through it, generating, on each iteration, all components it
contains.
As you can see at the top right, there is a Button called “Lend Stuff”, that is the action is
going to allow the user to enter a new record on the database. To do that, simply double-
click on the Button and use the RTL function redirect() to point the browser to another
page, in this case, addstuff.php:
Following the general design we used on the index page, place a top label with an “Add
Stuff” caption, then add a FBFriendSelector component and a Memo component below
that. The FBFriendSelector is like a regular Edit control, but it allows the user to choose a
friend just by typing some characters. We are going to use this control to let the user
select to which friend to lend the stuff. The stuff to lend is going to be entered in the
Memo component.
Also, two Buttons at the bottom: one to cancel the operation and return back to the index
page, and one to actually add the stuff to the database.
Embarcadero Technologies - 10 -
Creating Your First Facebook Application
If you run your application on Facebook, this is how the FBFriendSelector component
works:
Now, let’s make the page work by implementing the code for the Add button:
Embarcadero Technologies - 11 -
Creating Your First Facebook Application
To access that method, we need to add the global declaration to access the datamodule
object.
But wait, we have not create the addLendStuff() method on the datamodule yet. Here is
the code for it:
It simply appends a new record to the table, set the fields with the new values, and post it
to the server.
Embarcadero Technologies - 12 -
Creating Your First Facebook Application
DELETING STUFF
To delete stuff, we need to add that option in each detail of the DBRepeater control. To
do that, add a Label control inside the repeater control, change the caption to “Delete”
and generate the OnBeforeShow event. Now type in this code:
This code renders the Delete label as a link, with an URL specifying the action to be
performed and the ID of the record to be deleted.
To actually execute the delete action, on the OnBeforeShow event of the index page, we
should check if there is an action parameter to be executed and perform the operation.
Generate the OnBeforeShow event of the index page and type this code:
This code is executed before the page is shown. We can check for an action parameter on
input, and if that action is ‘delete’, then execute the deleteLendStuff() method of the
dmMain datamodule.
1. function deleteLendStuff()
2. {
3. $this->tbLendStuff->delete();
4. }
Embarcadero Technologies - 13 -
Creating Your First Facebook Application
This is just a call to the delete() method of the Table component, so how this component
knows the record to be delete?
Before opening the table, we have navigated to the correct record using the ‘id’
parameter on the request with the following code:
If there is an ‘id’ parameter, we are setting the Filter property to the right record, so a call
to delete() will delete it.
Embarcadero Technologies - 14 -
Creating Your First Facebook Application
CONCLUSION
In this paper, we’ve looked at how to create a Facebook application using RadPHP. We
saw how to create an application and database, adding different functionality including
user logins and lookups, and deploying the application on the Facebook network.
Hopefully this example application shows you how the visual development environment in
RadPHP can save you time and effort as you create PHP and Facebook applications. That’s
the idea behind “Rapid Application Development”, the “Rad” in RadPHP.
ABOUT RADPHP XE
Embarcadero® RadPHP™ XE revolutionizes PHP web development with a completely
integrated, rapid visual development approach and component framework. RadPHP XE
provides a powerful editor, debugger, visual development tools and connectivity with
leading databases. The integrated reusable class library includes components for
everything from UI design to building applications for Facebook. Visit the Embarcadero
web site to learn more about RadPHP XE and download a free trial.
Embarcadero Technologies, Inc. is the leading provider of software tools that empower
application developers and data management professionals to design, build, and run
applications and databases more efficiently in heterogeneous IT environments. Over 90 of
the Fortune 100 and an active community of more than three million users worldwide rely
on Embarcadero’s award-winning products to optimize costs, streamline compliance, and
accelerate development and innovation. Founded in 1993, Embarcadero is headquartered
in San Francisco with offices located around the world. Embarcadero is online at
www.embarcadero.com.
Embarcadero Technologies - 15 -