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 http://wiki.developers.facebook.com/index.php/Changing_profile_content). 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 http://wiki.developers.facebook.com/index.php/Infinite_session_howto:
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. http://fb.howtoforge.com/fb/htf_feed_reader/get_infinite_key.php).
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:
$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:
index.php looks like this:
rss.php looks like this (I've switched off the RSS cache for MagpieRSS here, but you can turn it on, if you like):
And finally, cronjob.php looks like this:
Now you can directly call cronjob.php in your browser (e.g. http://fb.howtoforge.com/fb/htf_feed_reader/cronjob.php) 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):
From now on, the profiles of the Facebook users that have installed your RSS reader application will be updated automatically.