Example of facebook iframe app using graph-API calls through php-sdk
Important notice! This post is probably outdated and my examples don't work due lack of "https".
This works now with PHP SDK (v.3.0.0) and up. I have changed this post after initial publish.
This is simple example of facebook iframe application which is not using FBML and it has link to authorise and gain access to "publish_stream" permission. It demonstrates how to use new php-sdk library and graph API for that. I was pretty much puzzled at first, because there was many ways and apis to achieve this.
Documentation and examples are confusing at moment to get started with apps. Especially when it comes to iframe app which has to ask permissions to access user data. Confusion is even greater because there is old way and the new way to do it. I wasted many hours to figure things out and get this basic sample working. Therefor I share this example and hope it saves hours for them who are also lost in documentation and examples.
Check it out
If user has not logged in or not given any permissions for app, it will show following contents. You could do automatic redirection, but I find this kind of approach much more user friendly because it can see (if presented) upfront what value this app promises.
After clicking allow link, it will direct user to permission request dialog. Notice the target parameter in that link. If it is not set "_top" it will show one extra (blank) FB page which is confusing.
After allowing this app, it will redirect back to previous page and this time we can do some interesting stuff. Like posting post on user's wall and accessing friends list. Of course there is much more you can do.
Set it up
So you want to try this out and start developing your own cool facebook application based on this. Here is quick step-by-step walk trough.
2. Unzip contents into your web server. e.g: http://www.example.eu/fb-app-test/ where index would be accessed eg: http://www.example.eu/fb-app-test/index.php
3. Download latest 'facebook.php' (php-sdk) from github and place it next to index.php and config.php
4. Go to Facebook developers app - allow access if needed
5. Find the "Create new app" button on the top and click it
6. Fill in essential information form and click "Continue" button.
7. After creating app you should be in editing view. Find submenu canvas settings in "On facebook" menu on the right side
8. Set Canvas Page to whatever you like. It will be your application's URL at facebook
9. Set Canvas URL. This must point to URL where you unpacked package at second step, without the index.php. e.g: http://www.example.eu/fb-app-test/
10. Set Canvas type to Iframe
11. Fill in config.php which variables should be pretty much self explanatory. Values can be found here after clicking on your app.
12. Save and you should be done. Go have a test-drive by entering newly created FB canvas url into browser.
There has been some changes in facebook after writing this post. My example didn't work (2011 april) but when I enabled "OAuth 2.0 for Canvas" in advenced tab, it started to work again. 2011 july edited my app to work with latest PHP SDK (v.3.0.0) (and up)