So you want to build a facebook app? The easiest thing to do is find someone else's code and modify it for your purposes. Especially since the facebook API documentation isn't really that good yet.
Unfortunately, Facebook Apps aren't automatically open, like say HTML or Javascript pages are. So you can't just look at the code of a working application. That's too bad. It would help a lot.
As it stands, the Facebook docs have a dearth of sample code for the Facebook API.
However, in the process of writing my first Facebook App, I spent a lot of time Googling (try looking for 'facebook.php' and other common include files on google.com/codesearch), reading forums, the facebook developer wiki and blogs.
So here's an omnibus post on where to find example code for Facebook
apps, and also a bunch of good links to posts that explain how to
write a Facebook app.
After you've looked thru Facebook "Hello World", you should download their simple, but instructive, Tutorial application called footprints. It's in PHP, and it comes as part of the client library that you need to use in PHP.
The biggest app out there which has released it's sample code is NewsCloud. It's open-source, and worth perusing, though it'd be a big job to try and get running.
In a application where you need to push data to facebook, for updating fb:ref handles, you may want to run your update code from a cron job. You still need to get a facebook api client, which means you have to login. You use your own infinite session key and user id in your code to do this. Here's a page on the facebook wiki which has some sample code for getting your infinite session key and using it in a cron job.
Here's a blog post on updating profiles with a code example using profile_setFBML.
Sample code in PHP connecting facebook to a social network "pledge" system
People Aggregator meta social network, with code in PHP
Code (PHP) from Facebook, which has a FQL call in it.
For those doing push updates with <fb:ref />, here's a re-written Facebook Rest Client class that uses curl_multi to parellize the refreshing of calls from fb:ref
A Perl / CGI example Facebook app - with an example of how to integrate login to a separate into a Facebook app
WWW::Facebook::API is good for Perl programmers, but also useful to just read through the code and docs and get an idea how they adapted the API for Perl.
A few bloggers have created posts which do a good job explaining how facebook apps work from a programming point of view. These are useful to read as you get started.
Here's a sample of their blogging about the architecture:
Facebook user's data in a generic fashion. The real advantage of this approach is that with just the UID of a Facebook user, I can load related data in a Facebook page or on the user's profile without having to do any processing on my end. For example, this --{% for friend in friends %} <fb:profile-pic uid="{{ friend }}" /> <fb:userlink uid="{{ friend }}"> <fb:name uid="{{ friend }}" useyou="false" /> </fb:userlink> {% endfor %}is an example in Django template syntax using FBML that would produce a list of friends with pictures.
Here are more tips and tricks to understanding the facebook platform from the devurandom guys. Some example code in Python in this post.
Excerpt:
Notice there is a "Callback URL" and a "Canvas Page URL". The callback URL is the base URL on the developer's server (washingtonpost.com in our case); the canvas page URL is the base URL on Facebook's server. When you install our app on Facebook, you are redirected to the canvas page URL, which in turn fetches content from the callback. You can have any number of callback pages extending off the base. If you went to apps.facebook.com/thecompass/foo/, then that page would fetch content from specials.washingtonpost.com/politicompass/foo/.Now you can't go directly to specials.washingtonpost.com/politicompass/ because without the POST data Facebook submits to the callback URL, the application won't work. If you hit our server directly without coming through Facebook, we redirect to the Facebook URL for our app. In fact, every time Facebook hits our callback URL there is a little setup that has to be done for each request
Sample FBML code and description of application architecture.
How to make a good social app so it fits in Facebook
Gotchas when using the platform. Part I. Part II.
Sample Hint on updating users' profiles:
To do a more periodic update, you can just iterate over all your users, get their uids, and call profile_setFBML() on each. Remember, from above, you don't need to call set_user for each, just use your own session_key.
- Spend time on your icons and screenshots - This is the first interaction your potential users will have with your app, so it had better be impressive, especially if you have competitor apps already out in the wild. There are three pieces to worry about: an application icon (16 x 16 pixels), an application logo (75 x 75 pixels) and a screenshot of any size you choose (which will be resized to the width of your About page). Make sure you have all of these ready to go before taking your app live.
Let me know if you find other examples and sample code, and I'll add it to this post.