The Urbano A. Company’s Blog
Writing a simple Facebook App
In this tutorial I am going to show you how to create a simple Facebook application.
Getting the basics
First of all you'll need to install the developer application on Facebook. To do this log in to your Facebook account and visit this link here to install the Developer application. You should be redirected to this screen:
Now you've installed the developer application you will need to create your first application. Click on the 'Apply for a key' and fill in the necessary details.
Give you application a name. In this example I have called mine 'My Test Application'. For the two email fields you should put in your email. Now for the callback URL. This is the directory on your host containing the files. Your application should definitely have it's own folder on your host (if not Facebook users will probably be able to access files you don't want them too). I've entered mine as http://www.urbanoalvarez.es/facebookApp/. All that's really left to do is your Facebook canvas page URL. I'm going to use aSimpleTestApp for mine. You can use anything you want as long as it's available. You shouldn't need to edit anything else. Just click 'Submit' to create the application.
You should have been redirected to the previous page only now you will see you have created an application. Note down the secret question, api key and your callback URL because you will need them in the next stage.
Now you've created your application you will need to install the Facebook Platform libraries on your host. You can download them here:
http://developers.facebook.com/clientlibs/facebook-platform.tar.gz
Once you've installed the libraries (read the README if you need any assistance) then you can start coding. First of all we need to create a config file which initiates the API and allows us to make a connection with Facebook.
The actual code
< ?php require_once 'client/facebook.php'; // Path to Facebook.php (either inside 'client' or 'php4client') $appapikey = 'apikey'; // Enter your API key you noted down earlier $appsecret = 'yoursecret'; // Enter your secret you noted down earlier $facebook = new Facebook($appapikey, $appsecret); // Initiate client $user = $facebook->require_login(); // Authorize current user $appcallbackurl = 'http://www.drcrazy4.info/fbapp/'; // Your callback URL // Has the logged in user installed the application try{ if ($facebook->api_client->users_isAppAdded() == ""){ // If the application hasn't been added $facebook->redirect($facebook->get_add_url()); // Redirect the user to the add applciation link } }catch (Exception $ex){ $facebook->set_user(null, null); // Clear current application cookies $facebook->redirect($appcallbackurl); // Redirect user to application } ?>
Save that file as config.php.
Now we have the authorization done we can get productive. For this application we're going to create a simple hello world application (I know how boring. In later tutorials I will discuss things like friends, photos etc.). Here is what's left:
< ?php include 'config.php'; // The file we made just now $echo = 'Hello there ' . $user; // $user is the logged in user. This will be displayed on the main application page $fbml = <<<EndHereDoc Hello world! EndHereDoc; // This is what will be displayed on users profile pages. $facebook->api_client->profile_setFBML($fbml, $user); // Set the profile box content for the logged in user ?>
Save that file as index.php.
Now test your application. You should see 'Hello world!' in a box on your profile and on the main application page you should see 'Hello [USERNAME]' ([USERNAME] being your current username). In later tutorials I will discuss a little more about FBML and how you can use the client libraries to get users friends etc. Tutorial source iscripting.com
If you have a question please feel free to ask.
| Print article | This entry was posted by alex on April 1, 2008 at 12:00 pm, and is filed under Facebook, PHP, Programming, Web-related. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
No comments yet.
New threat for all Joomla and WordPress installations
about 5 months ago - 2 comments
There is a new BOT out there, and one of the bad ones. I have started receiving traffic from it in my servers over the past week, and after some investigation it turns out it is quite a powerful bot, and so simple to use even a kid with a computer could use it.
The bot More >
Parse links in user comments
about 5 months ago - No comments
When you allow users to comment and post stuff to your website, it is interesting and useful allowing them to post links and other stuff. But how can we do so easily?
Surely there is BBCode, phpBB, allowing only some HTML tags… etc but how easy is this approach for the end user? Of course some More >
Calculate age in PHP from timestamp
about 6 months ago - No comments
If you ever wanted to calculate someone’s age in PHP from a birth timestamp, you must take into account that the age is more than the number of years, since days and months are also important, so I wrote a simple function that will return the exact age for a given timestamp:
function getAge($birth){
$t = time();
$age More >
Easiest PHP file upload
about 7 months ago - No comments
Hello people,
I want to share with all of you a file upload class I have developed, that makes it stupid simple to upload files haha
The PHP class:
First of all, here is the PHP class you will need:
< ?php
//Uploader class, by Alex
// This class is meant to handle all kinds of file uploads for DJs Music
// More >
Easily assign an image to a post in WordPress
about 1 year ago - 1 comment
Have you ever wondered how to assign an image to a certain post using WordPress? Surely there are some plugins that try to do this, and maybe they accomplish it, but probably slowing down your blog.
Well, here is a way of doing it without slowing the blog or installing any sort of additional plugins. When More >
Did you mean… ? In php
about 1 year ago - 13 comments
In a new website I am developing for a client I had to add the usual “Did you mean… ?” in the search results for her. Si I started thinking for the easiest way to do this.
There are actually a lot of php functions out there to look for similar text. The most obvious one?
similar_text()
You More >
How we became web developers…
about 1 year ago - 3 comments
I normally write to those web developers/programmers who are already good, experienced, and thus the articles are somewhat advanced.
But today I got up feeling nostalgic I guess, or I just felt like remembering back on my www birth, on my first impression of the Internet, my first site online, my first steps in w3c standards, More >
Using Gravatars in your blog comments
about 1 year ago - No comments
I’m sure you have seen in some blogs or websites that as soon as you enter your email, or other people do, an avatar is displayed for them. If you haven’t still set your own you should do that now.
A Gravatar is a “Globally Recognized Avatar”, meaning that anyone can display yours if they know More >
PHP easy image editing:
about 1 year ago - 3 comments
Do you have a picture upload and you don’t know how to easily resize/edit the uploaded images?
Well here is a solution for php that will make your life really easy!
It is called Asido, so you may go and download their code, to follow this tutorial.
First of all I’ll suppose you already know how to upload More >
Visitor’s language detection in php
about 1 year ago - No comments
Is your site translated into more than one language? In case it is, you will find this topic really interesting, since it will allow you to automatically adapt your site’s language to the user’s.
In case your site is not translated, you can always use this to know the visitor’s language… I’m sure you’ll be able More >
