Archive for February 4th, 2009

updating profile box using <fb:ref />

<fb:ref is a new fbml tag used to store data and later re-use it. there are two types of ref handler, one is static and another is url based. lets have a look at their structure

1. string handle which just stores FBML and later output the content – like


<fb:ref handle="application_specific_unique_handle" />

or

2. url based, which must serve valid FBML


<fb:ref url="some_url" />

you can create <fb:ref /> by using fbml_setRefHandle() function. now lets see <fb:ref />s in action. the following code will create a <fb:ref /> and you can later use it anywhere in your application. remember, once created the ref objects are in application scope. and until you change the content again using fbml_setRefHandle() api, it will remain the same.


<?php
include "config.php";
$handle = "greetings";
$markup = "Hello <fb:name uid='{$uid}' /> ";
$facebook->api_client->fbml_setRefHandle($handle, $markup);
?>

now you can use it anywhere in your application like this


<fb:ref handle="greetings" />

it will say "Hello <User’s Name>"

but wait, there is a major disadvantage with this type of handlers and that is the content of this ref object is cached. say, while creating the ref object if the $uid was, for example 503274632 (which is my id) – when I will come to a page that used this "greetings" ref object, I will see that the out put is "Hello you" and that is expected. Now when another user see the same page he will see that the output is "Hello Hasin Hayder", but this was not expected. Look, here it didn’t update the content according to the new user. Because the content of this ref object will remain cached until u change it again using fbml_setRefHandle() api.

to over come this problem and to make these ref objects more usable, you can use url type ref objects. lets see below, you have to use fbml_refreshRefUrl() api.


<?php
include "config.php";
$url = "http://sandbox.ofhas.in/fbtest/ru.php?uid={$uid}";
$facebook->api_client->fbml_refreshRefUrl($url);
?>

and the content of ru.php is


Hello from an external Url <fb:name uid="<?=$_REQUEST['uid'];?>" useyou="false" />

now you can use this url type ref object anywhere in your application, like this


Greetings! <fb:ref url="<?=$url;?>" />

now if you want to update content of your user’s profile box offline. Just print any url type ref handler in his profile box using profile_setFBML() api. later, on the time of updating all you have to do is call fbml_refreshRefUrl() so that facebook re cache the content of this url. and regardless to say that your url will deliver all the updated content to display on yoru profiles. your url will always get the user id via “uid” parameter in HTTP GET, so you can decide easily what to deliver.

if you are still confused or have any question, don’t forget to write it as a comment. in next article I am coming with “how to successfully keep record of all the users of your applications and how to remove them successfully when they remove your app” – stay tuned.

how to add profile tab for your facebook application

in new facebook design, users can add an application tab in their profile page along with 4 default tabs Wall, Infos, Photos & Boxes. In this tutorial we will see how we can add profile tab for our facebook application.
so, let’s go through couple of simple steps that will get this process done.
first of all, let’s keep something in our mind before we jump into it.

  • an application tab is 760 pixels wide. You can make your canvas pages as wide as an application tab if you want.
  • each application tab has a label. The labels are text only; a favicon cannot appear on the tab label at this time. by default, the tab uses the application name as the label

now, you have to register an application tab on facebook by specifying a tab URL in your application settings. go to your application settings> Edit Settings, from left nav select “User Profiles” – you there right? In the tab name, give something easy and related to your application name so that user can find it by search. In the second row, you are supposed to see “Tab URL”. In the box just enter a name of your page or the location (i.e. my_tab.php)

save the change and close the window. now create a new page called “my_tab.php”. open the page in your favorite IDE. now, let’s print something pretty simple to make sure our tab is working fine. you can print anything you want or you can copy & paste the following peace of code to see something on your tab page.


<?php $user = $_POST['fb_sig_profile_user'];
?>
hi <fb:name uid=<?=$user;?> linked='true' size='normal' useyou='false' firstnameonly='false'/>, glad to see your profile tab is working!

now, there is a common problem for those who added their profile tab before, they are getting this following message, although it worked before.
fb:redirect: redirect forbidden by flavor TabFBMLFlavor on the profile tab.
you are facing the same problem? Don t worry at all. Just check if you have this require log in in your profile tab page
i.e.:

  $user = $facebook->require_login();

if so, just remove it and instead that get the user’s id from “fb_sig_profile_user” parameter.
example:

user = $_POST['fb_sig_profile_user'];

so, to avoid this above problem don’t do the followings…

  • do not use $facebook->require_login() for profile tab views
  • do not use external javascript libraries for profile tab views

ok, I believe we are done creating a simple profile tab. Now, let’s check whether it’s working or not. go to your profile page and click on the “+” sign under your name. If you don’t see the label of your application then search with the label you gave on your application settings.

you supposed to see the message “hi, your_name, glad to see your profile tab is working!:)

Reference:
http://forum.developers.facebook.com/viewtopic.php?id=23956