FBML Caching

Problem

I’d like to take advantage of Facebook’s FBML caching to store a block of FBML that I use often.

Solution

Use the fb:ref tag. There are two ways to use it, depending on how you’ve architected your app:

By handle

If you’re developing heavily in the Facebook API, this is probably the better method for you. Use the API call setRefHandle to initialize your handle:

$facebook->api_client->fbml_setRefHandle("MyUniqueAppHandle", "Some FBML Content");

Then insert the fb:ref tag in your FBML to pull the block out and display it:

<fb:ref handle="MyUniqueAppHandle" />

Now when you want to update the content, just call setRefHandle again with new FBML and it will change everywhere you’ve put fb:ref tags.

By URL

If your app is based more on different files or pages on a server, you can still put FBML into the cache by giving it a unique URL. Create a page that only renders out the block you’d like stored, and then use the alternate form of the fb:ref tag:

<fb:ref url="http://www.someserver.com/cacheThisPage.php" />

When you want to change the content, update your page on your server and then make a simple API call to tell Facebook that it needs to reindex that URL:

$facebook->api_client->fbml.refreshRefUrl("http://www.someserver
.com/cacheThisPage.php")

Discussion

There are lots of places you might use this—really wherever you have a block of relatively static content you want to display—but the best use is probably for Profile Boxes that aren’t user-specific. In that case, you’ll want to ...

Get Facebook Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.