Helping ordinary people create extraordinary websites!

Create a Facebook Application With PHP

By Justin Laing
2008-01-07

Hello Facebook!

Let’s create a really simple first application that just says hello to the current Facebook user. Here’s the code for the Hello Facebook! application:

<?php
/* include the PHP Facebook Client Library to help
with the API calls and make life easy */
require_once('facebook/client/facebook.php');

/* initialize the facebook API with your application API Key
and Secret */
$facebook = new Facebook(YOUR_API_KEY,YOUR_SECRET_CODE);

/* require the user to be logged into Facebook before
using the application. If they are not logged in they
will first be directed to a Facebook login page and then
back to the application's page. require_login() returns
the user's unique ID which we will store in fb_user */
$fb_user = $facebook->require_login();

/* now we will say:
Hello USER_NAME! Welcome to my first application! */
?>

Hello <fb:name uid='<?php echo $fb_user; ?>' useyou='false' possessive='true' />! Welcome to my first application!

<?php

/* We'll also echo some information that will
help us see what's going on with the Facebook API: */
echo "<pre>Debug:" . print_r($facebook,true) . "</pre>";

?>

So what happens when a user hits the Canvas Page URL (from the example it would be: http://apps.facebook.com/makebeta/)? The require_login() call will produce a screen like this for the user:

Facebook Require Login Screen

If you change require_login() to require_add() the user will get a page that looks like this:

Facebook Require Add Screen

After the user logs into or adds the application they will get the canvas page with the “Hello…” text. It should look something like this:

Facebook Hello Screen





Tutorial pages:

Originally posted on Makebeta


 9 Votes

You might also want to check these out:


Leave a Comment on "Create a Facebook Application With PHP"
You must be logged in to post a comment.

Link to This Tutorial Page!


GET OUR NEWSLETTERS