How To Set Up A Facebook RSS Feed Reader Application For Your Blog - Page 5

7 Automatically Update The RSS Feed On The Profile

You might have noticed that the RSS feed on the profile gets updated only when you visit the application's canvas page in Facebook. This is a problem because people spend most of their time on the profile page. To automatically update the RSS feed on the profile page, we must therefore create a cron job on our server that automatically updates the RSS feed on the Facebook profiles.

There are two ways of updating profiles: direct and indirect (see If your content is individual for each user, you must use the direct method, but if it's the same for all users, it's much easier to use the indirect way. Because our RSS feed is the same for all users, we use the indirect way, for which we need an infinite session key for our application so that the cron job can log in to Facebook at any time.

To find out our infinite session key, we create the script get_infinite_key.php, as shown on

vi /var/www/fb/htf_feed_reader/get_infinite_key.php

// force a login page
$user = $facebook->require_login();

// Echo the "infinite session key" that everyone keeps talking about.
echo $facebook->api_client->session_key;

Now log out of Facebook in your browser and clear all cookies, or even better, use another browser in which you've never visited Facebook before (my default browser is Firefox, so I use SeaMonkey for this), and call that script in your browser (e.g.

You will then be prompted to log in to Facebook. Please make sure that you check the Save my login info to avoid logging in to Facebook again to use this application checkbox:

Afterwards, a page should show up showing your application's infinite session key. Please write it down somewhere:

Delete get_infinite_key.php afterwards:

rm -f /var/www/fb/htf_feed_reader/get_infinite_key.php

In the cron job script that I will use here, I will use the function $facebook->api_client->fbml_refreshRefUrl() to call a URL that delivers the HTML/FBML to put on the profile pages. This URL should deliver nothing but the HTML/FBML code; our index.php script delivers HTML/FBML code (for the application's canvas page in Facebook, using the echo $fbml; line), but it also changes the users' profiles with the $facebook->api_client->profile_setFBML() function, for which we'd need the user IDs of each user that have installed our app if we used it in our cron job script. We don't have these user IDs because we don't track them with our app, and as I said before, we want to use the indirect method to update the profiles.

Therefore I'll put the code for parsing the RSS and showing the HTML/FBML in a separate script, rss.php, which I will call in both index.php and our cron job script, cronjob.php. Furthermore, I'll create a configuration file (conf.php) for our app that contains all variable settings and that is included in all other scripts.

So let's start:

vi /var/www/fb/htf_feed_reader/conf.php
$appapikey = 'YOUR_API_KEY';
$appsecret = 'YOUR_SECRET';
$infinite_session_key = 'YOUR_INFINITE_SESSION_KEY';

$appcallbackurl = '';

$rss_url = '';

$feed = '';

$magpie_cache_dir = './magpie_cache';

$feed is the URL of our RSS feed, and $rss_url is the URL of our (yet to be created) rss.php file. Please don't mix these two up.

Now that we use conf.php, our new appinclude.php looks like this:

vi /var/www/fb/htf_feed_reader/appinclude.php

require_once 'facebook.php';

$facebook = new Facebook($appapikey, $appsecret);
$user = $facebook->require_login();

//catch the exception that gets thrown if the cookie has an invalid session_key in it
try {
  if (!$facebook->api_client->users_isAppAdded()) {
} catch (Exception $ex) {
  //this will clear cookies for your application and redirect them to a login prompt
  $facebook->set_user(null, null);

index.php looks like this:

vi /var/www/fb/htf_feed_reader/index.php



$facebook->api_client->profile_setFBML('<fb:ref url="'.$rss_url.'"/>', $user);

rss.php looks like this (I've switched off the RSS cache for MagpieRSS here, but you can turn it on, if you like):

vi /var/www/fb/htf_feed_reader/rss.php
//define('MAGPIE_CACHE_DIR', $magpie_cache_dir);
define('MAGPIE_CACHE_ON', 0);
//define('MAGPIE_CACHE_AGE', 600);

$rss = @fetch_rss($feed);

$fbml = '<fb:header decoration="no_padding">HowtoForge RSS Feed Reader</fb:header><div style="margin:0 10px 0 10px;">';
$fbml .= '<table border="0" width="100%" style="margin: 5px 5px 5px 5px;"><tr><td valign="top" width="80%"><a href="'.$rss->channel['link'].'" style="font-weight: bold;">'.$rss->channel['title'].'</a></td><td valign="top" width="80%"><fb:share-button class="meta">
  <meta name="medium" content="blog"/>
  <meta name="title" content="'.htmlspecialchars(strip_tags($rss->channel['title'])).'"/>
  <meta name="description" content="'.htmlspecialchars(strip_tags($rss->channel['description'])).'"/>
  <link rel="target_url" href="'.$rss->channel['link'].'"/>

foreach ($rss->items as $item) {
        $fbml .= '<div style="border-bottom: 2px solid #CCCCCC; padding-bottom:5px;"><br><div style="border-bottom: 1px dotted #CCCCCC; border-top: 1px dotted #CCCCCC;"><table border="0" width="100%" style="margin: 5px 5px 5px 5px;"><tr><td valign="top" width="80%"><a href="'.$item['link'].'" style="font-weight: bold;">'.$item['title'].'</a></td><td valign="top" width="80%"><fb:share-button class="meta">
  <meta name="medium" content="blog" />
  <meta name="title" content="'.htmlspecialchars(strip_tags($item['title'])).'" />
  <meta name="description" content="'.htmlspecialchars(strip_tags($item['description'])).'" />
  <link rel="target_url" href="'.$item['link'].'" />
        if($item['description']) $fbml .= $item['description'];
        $fbml .= '</div>';
$fbml .= '</div>';

echo $fbml;


And finally, cronjob.php looks like this:

vi /var/www/fb/htf_feed_reader/cronjob.php

require_once 'facebook.php';

$facebook = new Facebook($appapikey, $appsecret);

$facebook->api_client->session_key = $infinite_session_key;

// Now you can update FBML pages, update your fb:ref tags, etc.

Now you can directly call cronjob.php in your browser (e.g. to test if your RSS feed gets updated on your Facebook profile (of course, your RSS feed must be different than before...).

If everything is working as expected, you can create a cron job that calls cronjob.php every 30 minutes (or as often as you like):

crontab -e
0,30 * * * * /usr/bin/wget -O /dev/null &> /dev/null

From now on, the profiles of the Facebook users that have installed your RSS reader application will be updated automatically.

Share this page:

0 Comment(s)