As mentioned in my previous post, I’ve been finishing up a facebook application, My Sites. Now that it’s done, I thought I’d post a few tips on how to add profile boxes to your facebook application, since I found it maddeningly complicated.
For the non-facebook-savy, a profile box is a small box that applications can add to your profile’s “wall” or “boxes” tabs. They’re meant to provide a quick glimpse of what’s happening with the application.
I’m assuming that you keep a database of Facebook users, and you want to display something about them in the profile box.
The basic code for adding a profile box is below, annotated with copious comments. This is in PHP code, you can do it in other languages, but it’s what I use. Pop this code into a daily cron for example, and you’re good to go.
<?php
// This code connects to the facebook API.
@require_once 'facebook-platform/php/facebook.php';
$appapikey = 'facebook app api key';
$appsecret = 'facebook app api secret';
$facebook = new Facebook($appapikey, $appsecret);
// This code connects to your own database.
$BASEURL = "http://example.com/path/to/facebook/app";
$connection = @mysql_connect("server","username","password");
if(!$connection)die("Can't connect to the database at this time.");
$res = @mysql_select_db("database");
if(!$res)die("Site database doesn't seem to exist.");
// This code cycles through each user in the database
$sql="SELECT * FROM users";
$data=@mysql_query($sql);
while($user=mysql_fetch_assoc($data)) {
// You can, of course, query your database to build these strings
$WideBox = "Whatever you want a profile box on the 'boxes' tab to say.";
$MobileBox = "Whatever you want a profile box accessed by a mobile device to say.";
$NarrowBox = "Whatever you want a profile box on the 'wall' tab to say.";
// This is the API call, it sets the user's profile box content:
$facebook->api_client->profile_setFBML(
NULL, // This is the markup, set to NULL for 'FBML'
$user['id'], // ID number of the user whose profile box you want to set
$WideBox, // Wide box content for 'boxes' tab
NULL, // Deprecated, should always be NULL
$MobileBox, // Box content for mobile devices
$NarrowBox // Narrow box content for 'wall' tab
);
}
// Clean up the connection
@mysql_close($connection);
?>
