Of course, now that you've written the code for the application, you'll want to see what it looks like.

Viewing the new application

Start by typing your Canvas Page URL into a browser (you'll need to type in your own, but in the case of Pygoscelis P. Ellesworthy's Suspect Tracker, this would be http://apps.facebook.com/penguin_pi/):

You can then add the application just as you would add any other application:

And at last, you can view your new application:

It is worth noting, however, that you won't yet be able to view your application on your profile. For the time being, you can only access the application by typing in your Canvas Page URL. That being said, your profile will register the fact that you've added your application:

That's the obligatory "Hello World" done. Let's look at how to further develop the application.

Developing the simple application

Admittedly, that's not the most exciting application in the world, and certainly not one that's going to get you hundreds of users. However, it is enough for you to see the basics of a Facebook application. We can now look at advancing the application further.

So, let's see how to put some of the code into an application that we can actually use. You'll need to edit index.php again, and just as before, start by calling the application initiation file:

<?phprequire_once 'appinclude.php';

Next, we can introduce some FBML to create a personal welcome message:

The end result is that we've used <fb:profile-pic> and <fb:name> as well as HTML and PHP to produce a recognizable Facebook application:

Our application uses the $user variable that we obtain in appinclude.php:

$user = $facebook->require_login()

You'll realize, of course, that we're making use of the API in order to do this. So let's make further use of the API by obtaining the list of our user's friends and displaying their details in our application. Here, we're going to make use of $facebook->api_client->friends_get and $facebook->api_client->users_getInfo. However, we're not going to do all of our coding in index.php, instead we're going to add a function into appinclude.php. So, the first step is to define the function at the end of the file:

The end result is that less data is passed back to your application (which therefore gives a faster response time):

Of course, we can improve things even more by making use of FQL.

Using FQL in your application

If you look back at the code that we've just written, then you'll see that we accessed Facebook twice to obtain data. Once for the list of friends, and then for the friends' details. One of the advantages of using FSQL is that we can extract the data in a single transaction by making use of the $facebook->api_client->fql_query method:

If this FQL is used in our application, then we'll still see three columns of friends, but they'll only be those in England:

Of course, if you want to be really clever, you could convert the above code into a function, which when supplied with a country returns an array of friend IDs. However, I'll leave that to you. Instead, we'll turn our attention to the Facebook profile.

Here's a question: how many Facebook applications display something on the user's profile page? Well, at the moment, all of them, except yours. Let's rectify that now.

Writing to the Facebook profile

So far we've only been working with the application page; this means that you need to either access the application via the left-nav panel, or by typing the Canvas Page URL into your browser (e.g. http://apps.facebook.com/penguin_pi/). However, if you go to your profile page, you'll see:

Nothing.

And yet this is where your users will want to see your application, so that's a bit of a problem. Well, not really.

However, you may remember that Facebook keeps a cache, and it's this cache that's used to display information on a profile page. All we have to do is write to the cache.

Updating the Facebook Cache

So, we need to find a way to write the output to the profile, and we do this by making use of the API, and in particular, the $facebook->api_client->profile_setFBML method:

You'll notice that we need to write all of the FMBL to a variable ($fbml_text), and that we then use $facebook->api_client->profile_setFBML to produce an output for the profile.

You may also be wondering about the last line of code: why do we need to output text as well as the FBML? The reason is quite simple. If we run the application directly (by entering the Canvas Page URL) we'll get an error if there is no output at all. So our application needs to send the FBML to the Cache for the profile and produce an output for the application.

So, all you have to do now is type your Canvas Page URL into your browser:

And then go to your profile page to see the end result:

At this point, you're probably wondering if there's an easier way to refresh the FBML. After all, having to enter the Canvas Page URL, and then looking at the profile page to see the effect of your changes does take time. And of course, there is an easy answer. Just change the line:

print "Text written to Facebook Profile";

to:

$facebook->redirect($facebook->get_facebook_url() . '/profile.php');

How does this help? The line of code simply redirects the application back to the profile page. This means that instead of carrying out two steps (going to the Canvas Page URL and then back to the profile page), you can now just click on the link to your application in the left-hand navigation panel:

Once you click on your link (and the page has refreshed), you can just scroll down to your application and see the effect of your changes.

And while we're on it, we can also change the code to incorporate some of the work that we discussed earlier in the article. I had mentioned earlier that you can write a function to extract a user's friend's details by country, here it is:

Alerts & Offers

Series & Level

We understand your time is important. Uniquely amongst the major publishers, we seek to develop and publish the broadest range of learning and information products on each technology. Every Packt product delivers a specific learning pathway, broadly defined by the Series type. This structured approach enables you to select the pathway which best suits your knowledge level, learning style and task objectives.

Learning

As a new user, these step-by-step tutorial guides will give you all the practical skills necessary to become competent and efficient.

Beginner's Guide

Friendly, informal tutorials that provide a practical introduction using examples, activities, and challenges.

Essentials

Fast paced, concentrated introductions showing the quickest way to put the tool to work in the real world.

Cookbook

A collection of practical self-contained recipes that all users of the technology will find useful for building more powerful and reliable systems.

Blueprints

Guides you through the most common types of project you'll encounter, giving you end-to-end guidance on how to build your specific solution quickly and reliably.

Mastering

Take your skills to the next level with advanced tutorials that will give you confidence to master the tool's most powerful features.

Starting

Accessible to readers adopting the topic, these titles get you into the tool or technology so that you can become an effective user.

Progressing

Building on core skills you already have, these titles share solutions and expertise so you become a highly productive power user.