Posts Tagged with fbml:ref

Facebook <fbml:ref../>

For those of you who have embarked on writing a Facebook app, you may have faced the challenge of how to automatically update your profile fbml without forcing a user to take action. The approach I have found most effective is to use the <fbml:ref> tag. Facebook defines this command as a tag which “fetches and renders FBML from a given ref source – either a ref string “handle” you’ve created using fbml.setRefHandle or a URL that serves FBML. ” When using “fbml.setRefHandle” Facebook stores this FBML on its servers in the form of a hashtable. Using a single push we can update FBML for all users that have our application installed.

For a recent client we were asked to display the first few entries from a RSS feed within a users profile. All Facebook users would see the same entries, until the RSS feed changed. Here is the PHP we used to set a users FBML.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function render_ref_fbml($uid, $rssItems) { 
  global $facebook; 
  try { 
    // Set FBML Cache 
    $refFbml = render_narrow_fbml($id, $rssItems); 
    $refFbml .= render_wide_fbml($id, $rssItems); 
    $facebook->api_client->fbml_setRefHandle('RssFbml',$refFbml); 
    // Set Profile FBML 
    $fbml = render_css_style(); 
    $fbml .= '<fb:ref handle="RssFbml">'; 
    $facebook->api_client->profile_setFBML($fbml, $uid); 
    } catch (Exception $e) { 
      error_log($e->getMessage()); 
    } 
}

In a standalone php file we then created a similar function which updated only the FBML stored under the “RssFbml” handle.

1
2
3
4
5
6
7
8
9
10
11
function render_ref($rssItems) { 
  global $facebook; 
  try { 
    // Set FBML Cache 
    $refFbml = render_narrow_fbml(0, $rssItems); 
    $refFbml .= render_wide_fbml(0, $rssItems); 
    $facebook->api_client->fbml_setRefHandle('RssFbml',$refFbml); 
  } catch (Exception $e) { 
    error_log($e->getMessage()); 
  } 
}

This php file uses an infinite Facebook session and is called every 30 min by a CRON job.

1
30 * * * * php -f /var/www/rss.php