You might want to add the ability for your users to do something along the lines of your wall. There is functionality for this with the following:

  • <fb:wall> renders a wall-like section in your application that has <fb:wallpost> elements from your application users.
  • <fb:wallpost> is the message for the wall post that can contain an <fb:wallpost-action> element.
  • <fb:wallpost-action> adds a link at the bottom of the wall post content. Even if you put it at the beginning of the <fb:wallpost> element, the display will still render at the bottom of that particular post.

Walls are pretty easy to implement, assuming you have some type of persistence mechanism (such as an RDBMS). Assuming you do have an RDBMS, you would simply make a new table with three tuples (columns) to hold the UID (bigint), the actual post (text), and a time stamp (for indexing). Additionally, you could add a primary key field, though the time stamp should suffice for this. Now, all you need to do is loop over these results to provide them in the <fb:wallpost> tags, and all this should be wrapped in <fb:wall>. The only hard part is deciding how many posts you want to display on a page.

As a note, this set of tags is still in beta mode, but basically this is a mechanism to provide modal dialog boxes for your application. This is really a fancy pop-up box to interact with your users. If this tag doesn’t fit your needs, you can also use FBJS to create this type of interaction between your users by utilizing the Dialog object.
  • <fb:dialog> is the container tag for the dialog box.
  • <fb:dialog-title> is an optional title for your dialog box.
  • <fb:dialog-content> is the message contained in the dialog box.
  • <fb:dialog-button> renders a button for the dialog box.

Consider the following FBML snippet for constructing a dialog box:
<fb:dialog id="fb_test">
<fb:dialog-title>This is a test</fb:dialog-title>
<fb:dialog-content>Content</fb:dialog-content>
<fb:dialog-button type="button" value="Okay" close_dialog="1" />
</fb:dialog>
<a href="" clicktoshowdialog="fb_test">show fb:dialog</a>

The <fb:dialog> snippet will render a modal window as shown in the below Figure. Within the <fb:dialog-content> tag, you are also able to add other information (and other FBML) tags, such as forms to perform more advanced interactions with your users.

Figure: FBML <fb:dialog>

For example, take this snippet that generates a search form to search Facebook (or some other site):

<fb:dialog id="fb_search" cancel_button="true">
<fb:dialog-title>Search Facebook</fb:dialog-title>
<fb:dialog-content>
<form action="
http://www.facebook.com/s.php" method="get">
<input type="text" name="q" />
<input type="submit" value="Search" />
</form>
</fb:dialog-content>
</fb:dialog>
<a href="" clicktoshowdialog="fb_search">Show Search</a>

Now, when the user clicks the Show Search link, a modal window will pop up, as shown by the below Figure. When users hit the Search button, they are passed to the new server, which in this case presents users with their search results.

Figure: <fb:dialog> with form.

As mentioned previously, you can make these dialog boxes using FBJS (using the Dialog object). However, not everyone is a JavaScript expert, so the <fb:dialog> tags provide a convenient method to do most of the same things you can do with FBJS without writing any FBJS.

Once you have your application completed, you’re going to want to develop a navigation scheme for your users. The main tag for this task is the <fb:dashboard> tag that builds the familiar dashboard layout in Facebook. There are additional tags that you can lay out within the <fb:dashboard> tag, including buttons, hyperlinks, and even help:

  • <fb:dashboard> renders the standard Facebook dashboard for navigation. This is a container tag for <fb:action>, <fb:help>, and <fb:create-button>. Note that you cannot use the <fb:if-user-has-added-app> tag inside this tag.
  • <fb:action> is analogous to an anchor tag for the dashboard.
  • <fb:help> creates a link to the application’s help. This is rendered to the right side of the dashboard.
  • <fb:create-button> creates a button for in the dashboard.
  • <fb:header> renders a title header.
  • <fb:media-header> renders a media header. This tag is generally used to display user-contributed content to specific users.
  • <fb:tabs> is a container to add tabbed-navigation style of links to your application. Individual tab items are added with the <fb:tab-item> tag.

The tag allows you to nest <fb:action>, <fb:help>, and <fb:create-button> tags:
<fb:dashboard>
<fb:action href=".">Add Something</fb:action>
<fb:action href=".">Delete Something</fb:action>
<fb:help href=".">Help me</fb:help>
<fb:create-button href=".">Add Something</fb:create-button>
</fb:dashboard>

The <fb:tabs> tag, by contrast, allows only the <fb:tab> tag to be nested:
<fb:tabs>
<fb:tab_item href="." title="Add Something" />
<fb:tab_item href="." title="Delete Something" />
<fb:tab_item href="." title="Help Me" />
</fb:tabs>

Both of these tag sets provide different functionality to you. Typically you will use <fb:tabs> for creating an overall navigation schema, and you will use <fb:dashboard> for performing functions within your application.


To work with editing data, Facebook has derived a set of tags grouped in this section. The rendered form will display in two columns with the label on the left and an input field on the right. The one caveat to using the <fb:editor> tags to create forms is that you cannot use mock Ajax. If you want to be able to use mock Ajax, you will need to manually create your own form.
  • <fb:editor> is the outermost tag used to create an editable form.
  • <fb:editor-button> creates a button for your form.
  • <fb:editor-buttonset> creates a container for one or more buttons.
  • <fb:editor-cancel> creates a cancel button for the form.
  • <fb:editor-custom> allows you to insert whatever code you want, as long as it’s valid FBML.
  • <fb:editor-date> creates two select boxes in the form for the month and day.
  • <fb:editor-divider> adds a horizontal line divider to your form.
  • <fb:editor-month> creates a select box populated with the months of the year.
  • <fb:editor-text> creates an input box for your form.
  • <fb:editor-textarea> creates a textarea box for your form.
  • <fb:editor-time> creates select boxes for hours, minutes, and an a.m./p.m. indicator for your form.

As an example of this usage, consider the following.

<fb:editor action="." labelwidth="100">
<fb:editor-text name="input" label="Text" />
<fb:editor-textarea name="textarea" label="Editor Text Area" />
<fb:editor-custom label="Select">

<select name="select">

<option value="editor-custom">Editor Custom Select</option>

</select>

</fb:editor-custom>
<fb:editor-divider />
<fb:editor-date name="date" label="e" />
<fb:editor-month name="month" label="h" />
<fb:editor-time name="time" label="e"/>
<fb:editor-buttonset>

<fb:editor-button value="Add"/>

<fb:editor-button value="Edit"/>

<fb:editor-cancel />

</fb:editor-buttonset>

</fb:editor>

Remember, the form the tag produces uses the Post method. If you use the tag, you will need to write some code on your server to then do something, but the purpose of this example was to show how to use these tags in conjunction with one another to create a form. In this case, the previous snippet will render as depicted in the below figure.


Here are some miscellaneous tags:

  • <fb:js-string> allows you to define a string to reference in FBJS. You can set this anywhere in your code, and it is not displayed to the user. For example: <fb:js-string var="foo">This is the rendered text</fb:js-string>.
  • <fb:fbml> allows you to define the specific version of FBML. Currently, the valid versions include 1.0 and 1.1.
  • <fb:fbmlversion> displays the version of FBML that is currently being used.
  • <fb:redirect> redirects the browser to another URL in your application.
  • <fb:ref> allows you to define FBML that resides at a specific URL that you then call through the tag. This is generally used when you want to update a lot of profiles without publishing the data on a per-user basis.
  • <fb:share-button> creates a share button with either URL information or specific metadata.
  • <fb:time> renders a time in the user’s time zone.
  • <fb:title> sets the page’s title tag.

Working with form information is important in developing any online application. FBML has some constructs to help with these common tasks.

  • <fb:request-form> creates a form for sending requests. This is a great way to let your users send requests to add the application (when used with the <fb:multi-friend- seletor> tag). You cannot use this tag if you are using iframes.
  • <fb:request-form-submit> creates a submit button for use with the <fb:request-form> tag.
  • <fb:multi-friend-input> renders a multifriend input field like the one used in the message composer.
  • <fb:friend-selector> renders an autocomplete input box of the user’s friends.
  • <fb:submit> creates a JavaScript submit button for your forms. This is generally used when you want to use an image instead of a submit button, such as <fb:submit><img src="path/to/image" /></fb:submit>.

Tag-based languages such as ColdFusion and JSTL have many tags that build portions of your application for you। Similarly, FBML has a set of tags to help you build some very useful portions of your application:

  • <fb:comments> generates code to add comments to an application। It takes care of posting and redrawing pages that are posted to for a given UID.
  • <fb:google-analytics> adds the JavaScript to your application so you can use Google Analytics to track your application usage.
  • <fb:mobile> displays content for mobile users (http://m.facebook.com/)। Content outside of these tags will be ignored for mobile users.
  • <fb:random> randomly rotates certain parts of your application content (for quotes, pictures, or even advertising). This tag not only can randomly choose an element from within the tag (defined by the <fb:random-option> tag) but also can weight these options to appear more often (or less often) than other options.

You might encounter instances in which you want to display specific content to your users depending on their profile status with your application। FBML allows you to distinguish between the application owner, users, application users (those who have granted full access to your application), and those who have added the application to their accounts।

  • <fb:visible-to-owner> displays content if the user is the owner। As a side note, if the user isn’t the owner, this displays whitespace।
  • <b:visible-to-user> displays content to the specified user।
  • <fb:visible-to-app-users> displays content if the user has granted full permissions to the application।
  • <fb:visible-to-added-app-users> displays content if the user has added the application to their account.

Rich media is one of the cornerstones of the modern Internet…just look at YouTube. If you find a need to use embedded media in your application for music, games, or other rich media, you can use several tags to do this. This is an area in which FBML diverges from HTML because it is missing an <embed> tag। However, you are still able to use this functionality through the following tags:
  • <fb:iframe> inserts an iframe into your application to pull in external sources to your application। This tag cannot be used in the profile page.
  • <fb:photo> renders a picture that is in the Facebook repository and the user has permission to view।
  • <fb:mp3> adds a Flash-based MP3 player that controls the MP3 file specified। Remember, this has to be the absolute path to the file.
  • <fb:swf> renders a Flash object on the page of the specified absolute path। On profile pages, the SWF file is rendered as an image and rendered directly on canvas pages.
  • <fb:flv> renders a Flash-based player to stream FLV content. This tag will use only .flv extensions, not other formats such as AVI।
  • <fb:silverlight> is basically the same as the <fb:swf> tag, but for Microsoft’s Silverlight-based content.

You might find that you need to provide different content depending on where your users are accessing the application from। Facebook provides the following tags for displaying content inside the user’s profile box:

  • <fb:wide> displays content only when the profile box is the wide column।
  • <fb:narrow> displays the content only when the profile box is the narrow column।
  • <fb:profile-action> builds a link on the user’s profile under their photo। You’ll use this in conjunction with setFBML to send information to the user’s profile. As a note, there is a 30-character limit for the contents of this tag.
  • <fb:user-table> renders a table of the users (specified by the <fb:user-item> tag) you have specified। This tag works only on a user’s profile page (will not render on the canvas page).
  • <fb:user-item> defines a user for use inside the <fb:user-table> tag।
  • <fb:subtitle> defines a subtitle for the profile box.

Working with your user’s and group’s information is an important part of any Facebook application। You want to let your users easily interact with other users of your application, and there are some specific FBML tags to help with these interactions:

  • <fb:name> returns the user’s name for the uid passed to the tag। This function has a lot of customizable features to allow you to display the possessive of the user’s name and additional logic to handle users who have protected their profiles. For example, the <fb:name uid="$user" ifcantsee="Anonymous"> tag returns “Anonymous” if the user has chosen not to show their name in their profile.
  • <fb:grouplink> returns the name and a link of the group ID passed to the tag।
  • <fb:user> displays content to the specified user and no one else।
  • <fb:pronoun> renders a pronoun for a specific user। This is a fun tag to use because it has several attributes that let you choose the different formats of the pronoun’s use, including possessive, reflexive, and objective forms.
  • <fb:profile-pic> renders a linked image of a user’s profile picture। By default this is a 50-by-50-pixel image. This is good for “iconifying” your user’s interactions.

FBML contains many conditional tests that can help you cut down on implementing this code in your application. At the heart of these conditionals is the <fb:if> tag:.
<fb:if value="true">
<p>Hi</p>
</fb:if>
At first glance, this isn’t that useful because the value attribute will always be true. This is where your programming language comes into play. To actually make this do something useful, you need to be able to dynamically set this value. Let’s write a short test to see whether the logged-in user has a user ID of 12345 and show a customized message:
<?php
$user = $facebook->require_login();
$test = false; // you may also use 1/0 for true/false
if($user == 0000001){
$test = 1;
}
?>
<fb:if value="<?php echo($test)?>">
<p>This is the secret message.</p>
<fb:else>
<p>No secret message for you!</p>
</fb:if>
This is a nonsense example, but it shows how you can you use the <fb:if>/<fb:else> construct to display custom messages to your users. You will find that through your application development process you will start constructing more complex <fb:if>/<fb:else> statements. Fortunately, the developers of the Facebook platform anticipated this and have a set of tags that will do many of the most common types of conditional checking.
As I stated earlier, Facebook tags act differently in different sections of the web pages. These conditional checks can occur only on the canvas page of your application:
  • <fb:is-in-network> displays content if a UID is in the specified network.
  • <fb:if-can-see> displays content if the logged-in user can view the specified content. This is often used to implement your own privacy features in your applications.
  • <fb:if-can-see-photo> displays content if the user is logged on and has permissions to view the photo specified.
  • <fb:if-is-app-user> displays content if the specified user has accepted the terms of service for the application.
  • <fb:if-is-friends-with-viewer> displays content if the user specified is friends with the logged-in user.
  • <fb:if-is-group-member> displays content if the user is a member of the specified group.
  • <fb:if-is-own-profile> displays content if the viewer is the profile owner
  • <fb:if-is-user> displays content if the viewer is one of the specified users.
  • <fb:if-user-has-added-app> displays content if the specified user has added the application to their account.

Unfortunately, there isn’t an FBML construct for else if logic. If you need to perform multiple conditional checks, you will need to properly nest your if statements. Alternatively, you can use the FBML’s switch construct.

The FBML <fb:switch> tag acts a bit differently than many programming languages that implement the construct. In FBML, the <fb:switch> tag evaluates a set of FBML tags and returns the tag information from the first tag that returns something other than an empty string:

<fb:switch>

<fb:user uid="0000001" />

<fb:default>This is the default statement</fb:default>

</fb:switch>


This code will display the contents of the <fb:default> tag since there’s no user with a UID of 0000001. You may at some point need something a bit more complex for your tests. You are able to nest <fb:if> and <fb:switch> statements within an <fb:switch> tag for these more advanced conditional analyses in your code:

<?php

$user = $facebook->require_login();

$test = false;

if($user == 0000001){

// Boolean true = 1

$test = 1;

}

?>

<fb:switch>

<fb:if value="<?php echo($test)?>">

<fb:switch>

<fb:profile-pic uid="<?php echo($user)?>" />

<fb:default>Inner default</fb:default>

</fb:switch>

</fb:if>

<fb:default>Outer Default</fb:default>

</fb:switch>

As you’ve probably noticed, there’s no case statements with breaks that you normally see in other programming languages. If you’re familiar with the switch statements having case and break statements, just think of each tag as a distinct case with no need for a break statement. There are times where this could require more complex nesting of statements, but if you find your conditional statements getting too complicated, it’s probably a good idea to take a step back from what you’re doing and see whether you can find an alternative method to perform the same check. Also, as a programming note, the switch statement essentially has a break after the first true statement in the switch statement. If you place the <fb:default> tag at the top of your code block (which will always return true), the rest of your switch statement will not get evaluated.

FBML-specific tags are really the meat of the Facebook platform. The tag set isn’t overly complex, but it has already gone through two iterations with FBML 1.0 and FBML 1.1. This change actually raises a sometimes-frustrating aspect in how Facebook changes the platform. When FBML 1.1 was announced in August 2007, developers basically had ten days to make their code compliant to the new specification. It is imperative that if you’re developing an application for Facebook that you keep up with the changes to the platform so your application doesn’t stop working. If you haven’t already subscribed, add the Facebook News feed (http://developers.facebook.com/news.php?blog=1) to keep abreast of changes.

I’ll also take a moment here to talk briefly about some of the issues, err, enhancements that you will see when using FBML. One of the big things you’ll notice is that there are FBML tags that will act differently in different locations. As an example, you can use iframes on canvas pages, but you cannot use the same iframe on the code in the profile box. There is also a queue of requested tags that are being considered for inclusion with the next FBML tag set iteration. Although not all of these tags will make it into the official language, it’s interesting to see what the developer community is requesting to be included. You can view and add to these requests at http://wiki.developers.facebook.com/index.php/Requested_FBML_Tags.

The developer’s wiki for the Facebook platform groups the tags by their function. I believe this is perhaps the most useful way to work with the FBML because of the sheer volume of tags (almost 100 as of version 1.1). Also, because of this volume, some tags will necessarily have more information about them than others. If you find some of these descriptions and examples insufficient, please refer to the official documentation for the tags.
FBML tags are set apart from other HTML tags with the fb prefix and follow the format .
<fb:tag_name>.

For the most part, most commonly used HTML tags will work on the Facebook platform. If you’ve worked with HTML in the past, you’re already familiar with this part of the platform. One major difference between typical HTML and FBML is that “normal” JavaScript is stripped from your code. For instance, you cannot use the onclick attribute in the anchor (<a>) tag to call JavaScript:
<p>
<a href="javascript:alert('You\'ll never see me');">click me</a>
</p>
Although completely valid HTML and JavaScript, the previous will raise an error when your users look at the page containing this code.
Don’t worry, if you need access to JavaScript for your application, Facebook has developed FBJS, which will allow you to use many of the conventions you typically see in JavaScript.
When working with FBML, remember that it’s not exactly HTML, even though you use a lot of the same syntax. Your code has to be processed through the Facebook platform to ultimately generate the HTML that gets rendered to the user, so not everything you’re used to doing with HTML code will work.

The Facebook Markup Language (FBML) is the heart of the Facebook platform. You might see some folks referring to FBML as “fancy” HTML tags, but it actually does a little more than static HTML because it has a dynamic connection to the Facebook back end. If you have developed any web applications in ColdFusion or JSP (using JSTL), programming with FBML will be very familiar. The Facebook Markup Language is described on the wiki site as an “evolved subset of HTML,” so you have many of the same tags available to you as you do in normal HTML, but you also get a much richer tag set that allows you to code myriad interactions with the users very quickly.

Although the FQL language isn’t ANSI-SQL complete, it does have some simple operators and functions to help you work with user data. FQL has boolean operators (AND, OR, and NOT), comparison operators (=, >, >=, <, <=, <>), and arithmetic operators (+, -, *, and /). The functions included in FQL, although not exhaustive, allow you to perform some basic string manipulations. To conserve bandwidth, you can use the concat() function to group several tuples together:

<?php

$facebook_config['debug'] = false;

$facebook_config['api_key'] = '<your_api_key>';

$facebook_config['secret_key'] = '<your_secret_key>';

require_once('<path_to_client_library>/facebook.php');

$facebook = new Facebook($facebook_config['api_key'],

$facebook_config['secret']);

/**

* Ensure the user has logged on to Facebook

*/

$user = $facebook->require_login();

/**

* Construct the FQL request

*/

$fql = "SELECT concat(first_name, ' ', last_name)

FROM user

WHERE uid = '$user'";

/**

* Pass the FQL to Facebook through the API client

*/

$fql_result = $facebook->api_client->fql_query($fql);

/**

* Print the results to the screen

*/

echo "<pre>FQL Result:" . print_r($fql_result, true) .

"</pre>";

?>

The previous example simply selects the first and last names of the user who is currently making the request. The resulting page will display an array in the following format:

FQL Result:Array

(

[0] => Array

(

[anon] => <your_first_name> <your_last_name>

)

)

You might be saying to yourself, “This is pretty useless. What’s the difference between this and just calling both the fields?” Well, if you have any bandwidth concerns, you can alleviate some of those issues by using the concat function to put fields that you need together. For instance, you might want to put a specific string into your page that combines several fields in a specific way. Letting the Facebook servers do some of this processing before it gets back to your server will not only decrease your server load but can also cut down on your bandwidth in order to speed up your application.


Not only can you do simple SQL-style selects, but you can also perform subqueries. Take, for example, this FQL equivalent for the facebook.events.get REST API call:

SELECT eid, name, tagline, nid, pic, pic_big, pic_small, host, description,

event_type, event_subtype, start_time, end_time, creator, update_time,

location, venue

FROM event

WHERE eid IN (SELECT eid FROM event_member

WHERE uid=uid AND rsvp_status=rsvp_status) AND

eid IN (eids) AND

end_time >= start_time AND

start_time < end_time

I won’t go into the theory behind nested queries, but I will mention that they are very useful for testing set membership, set comparisons, and set cardinality. And, this expansion of the REST call serves as a good example for writing your own custom FQL expressions.

You might find that you will need to take some additional processing to make sure your information is displayed in a specific order. You’ll have to do this with your client language. For PHP, you need to sort the array and then slice it. Let’s take the following:

<?php

$fql = "SELECT eid, name, location

FROM event

WHERE eid IN (

SELECT eid

FROM event_member

WHERE uid = '$user'

)";

$fql_result = $facebook->api_client->fql_query($fql);

asort($fql_result);

array_slice($fql_result, 0, 5);

?>


The previous passes an FQL query to find events for the current user, sorts the resulting PHP array, and returns the array with the six elements (positions 0–5) of the query result.

FQL allows you as a developer to have granular control of the information that you retrieve about your users from the Facebook servers. Although it’s not as robust as you might sometimes need (or want) it to be, you can generally get around FQL’s limitations with some post-processing in the language in which you’re developing. Additionally, as the complexity of your FQL increases with subqueries, you might at some point run into problems. As I’ve mentioned earlier, using the Facebook API Test Console at http://developer.facebook.com/tools.php is a great place to help debug your code. For instance, if you take the previous query and take out the WHERE clause so that your FQL statement reads as follows:

SELECT eid, name, location

FROM event

WHERE eid IN (

SELECT eid

FROM event_member

)
then, when executed, this will raise an error because you must have a limiting WHERE clause. If you missed it when you look at your code, the resulting XML response shows an error code of 601 and an error message of “Parser error: WHERE clause is required.” Fortunately, this is an easy fix, but you might find yourself working with more complicated interactions with FBML and FQL, and this tool can provide invaluable help in discerning where your bugs exist.

Here’s a list to make sure you know what’s available to you in the different tables. (OK, these aren’t really tables; more likely these are views of specific data, but for simplicity’s sake, we’ll just call them tables.)

  • users(uid, first_name, last_name, name*, pic_small, pic_big, pic_square, pic, affiliations, profile_update_time, timezone, religion, birthday, sex, hometown_location, meeting_sex, meeting_for, relationship_status, significant_other_id, political, current_location, activities, interests, is_app_user, music, tv, movies, books, quotes, about_me, hs_info, education_history, work_history, notes_count, wall_count, status, has_added_app)
  • friend(uid1, uid2)
  • group(gid, name, nid, pic_small, pic_big, pic, description, group_type, group_subtype, recent_news, creator, update_time, office, website, venue)
  • group_member(uid, gid, positions)
  • event(eid, name, tagline, nid, pic_small, pic_big, pic, host, description, event_type, event_subtype, start_time, end_time, creator, update_time, location, venue)
  • event_member(uid, eid, rsvp_status)
  • photo(pid, aid, owner, src_small, src_big, src, link, caption, created)
  • album(aid, cover_pid, owner, name, created, modified, description, location, size)
  • photo_tag(pid, subject, xcoord, ycoord)

If you’ve worked with SQL before (and I assume you have), FQL isn’t a big deal. You use the same syntax as typical ANSI-SQL, and there are only nine tables to deal with. There are, however, some important differences. There are no joins, and FQL has not implemented the LIMIT, GROUP BY, or ORDER BY clauses that are common to ANSI SQL–compliant implementations.

Facebook also has implemented an API for basic database manipulations with its Data Store API. This API provides a basic create, read, update, and delete (CRUD) API for storing data that you access through REST. If you’re unfamiliar with object-oriented database management systems (OODMSs), some of the terminology is a bit different from that for relational database management systems (RDBMSs). For instance, to use the Data Store API, you must define your schema (your database), which consists of object types (tables) and properties (columns).

One of the really nice features of the Facebook Data Store API is that Facebook does not plan to charge for normal use! You basically have use of Facebook’s servers to perform your database manipulations for you. However, it’s probably not quite time to get rid of your RDBMS yet, because there aren’t any structured queries, full-text search, or transaction-level query processing in the Facebook Data Store API.

Note:
Data Store API may be in beta stage. Please consult the wiki documentation for the latest information before you deploy any projects using the Data Store API.

The Data Store API consists of three basic functions: specialized tables, distributed tables, and associations that are split into five separate APIs (User Preference, Object Data Definition, Object Data Access, Association Data Definition, and Association Data Access). Since Facebook provides access to millions of users, the tables (objects) you create are distributed. Facebook does provide a specialized table of users that is optimized (if you find that you really need more, let the Facebook developers know at their Bugzilla site, http://bugs.developers.facebook.com/). The associations component of this API is a mechanism to provide performance for fast lookups (such as indexes). Because indexing tables in a distributed environment won’t necessarily provide a performance boost, this mechanism has been implemented to provide fast lookups without centralized indexes or parallel queries.

The user preferences for the API consist of 128-character strings, for which you can store up to 201 for each user (numbered 0–200). Access to the getters/setters are accessed through getters and setters in the REST API (facebook.data.setUserPreference and facebook.data.getUserPreferences).

Data objects (that is, tables) are created with facebook.createObjectType. The object type takes a name and contains a set of object properties (that is, columns). Object properties have names and data types. You don’t quite have the same type of control over the data types with the API as you do with your own RDBMS because you are limited to integers, strings (less than 256 characters), and text blobs (with a maximum of 64KB).

After defining your objects and object types, you create, read, update, and delete through the Object Data Access API. These are rather straightforward (facebook.data.createObject, and so on).

To work with the associations between objects, you first need to define the relationship between objects in the facebook.defineAssociation call. You can define two types of associations: one-way, symmetric associations and asymmetric associations. If you’re familiar with RDBMS joins, think of an asymmetric association as a many-to-many join and a symmetric association as a one-to-many join. One-way associations are an association between objects that can happen only one way (in other words, there’s no need to look up a value by some ID) for a given object. You then create the actual associations with the Association Data Access API. These methods allow you to create, remove, and retrieve these associations and retrieve the object details from the data contained in the data definition.

This can be confusing at first, so let’s look at an example:
<?php
$createObject = $facebook->api_client->data_createObjectType("wallpost");
$uid = $facebook->api_client->data_defineObjectProperty("wallpost", "uid", 1);
$time = $facebook->api_client->data_defineObjectProperty("wallpost", "timePosted",2);
$post = $facebook->api_client->data_defineObjectProperty("wallpost", "post", 3);
?>

The previous snippet of code is analogous to the following SQL DDL:
CREATE TABLE wallpost(
uid integer,
timePosted timestamp,
post text
)

As you can see in this simple example, this can take a little bit to get used to because you’re not performing your typical SQL DDL; however, once you get your mind around how to create the objects, it’s relatively trivial to use the API as the persistence layer for your application. I suspect that this API will eventually make it out of beta and be quite a powerful tool in the Facebook developer’s toolbox, at least for those who choose to have Facebook manage their data.

Sometimes when you’re developing an application, you make mistakes. Fortunately, Facebook returns rather robust error messages when something goes wrong (like you forgot to provide a specific parameter for an API call). The error codes are returned with both a numeric value and a message. Generally, if you receive an error message (in a structure), it’s rather obvious when you read the error message (in the err_msg element). If you can’t figure out what’s going on with a specific call, it’s always a good idea to check out the code in the API Test Console (http://developer.facebook.com/tools.php). Although this won’t give you any more information than you are getting returned, it can help you narrow down what’s going on (in case you have multiple errors).