Getting Facebook Developer Application running and libraries installed:
Go to http://developers.facebook.com/ . Click the Green “Get Started” Button
Click “PHP Client Libraries” on the right side of the page in the gray box. Save the zip file to your local computer. Unzip the contents of “facebook-platform”>>”php” and upload to the folder on your web server that will serve as the root of your Facebook application. FYI, you just need the files in the “php” folder in your root directory.
Install the Facebook developer application at http://www.facebook.com/developers/
Click the “Setup a new application” link in the gray box in the upper right of the developer application homepage
Fillout the application name and submit
Now you are on the edit application settings interface.
Click the “Canvas” tab on the left and change the following:
- “Canvas Page Url” enter the url you want your application to be displayed under on facebook. I used “iframexample”.
- Under “Canvas Callback URL”, enter the URL of the applications “homepage” on your server. I used “www.keywordintellect.com/facebook/iframeexample/”.
- Under “Canvas Settings” click “IFrame”.
Click the “connect” tab on the same settings interface. In the “Connect URL” field enter your Canvas URL (http://www.keywordintellect.com/facebook/iframeexample/ in my case)
Save your settings
Setup your canvas page:
Create a file named “index.php” and put it in the root directory on your webserver for the application “www.keywordintellect.com/facebook/iframeexample/” in my case.
Call the Facebook libraries and authenticate. Note: Replace the app key and secret with the values displayed on your applications settings page withing Facebook. Note: that since I have many aps running, I separated out the Facebook Libraries in a separate directory up one level from my application roots
<?
//Link in library. Note my directory structure may be different
require_once '../fblibrary/facebook.php';
//Authentication Keys
$appapikey = 'REPLACE WITH YOUR KEY';
$appsecret = 'REPLACE WITH YOUR SECRET';
//Construct the class
$facebook = new Facebook($appapikey, $appsecret);
//Require login
$user_id = $facebook->require_login();
?>
Let’s also add in a basic XFBML call to the fb:name tag as well as an API call to get the UIDs of the logged in users friends
<?
// XFBML: Greet the currently logged-in user!
echo "<p>Hello, <fb:name uid=\"$user_id\" useyou=\"false\" ></fb:name>!</p>";
// API: Print out at most 25 of the logged-in user's friends, using the friends.get API method
echo "<p>Friends:";
$friends = $facebook->api_client->friends_get();
$friends = array_slice($friends, 0, 25);
foreach ($friends as $friend) {
echo "<br>$friend";
}
echo "</p>";
?>
Now, if we were to stop here and run it, the API call would work, but the XFBML would not. We need to set up the page to handle the XFBML. The XFBML documentation is here: http://wiki.developers.facebook.com/index.php/XFBML.
First, you need to create a Cross Domain Communication Channel File. Follow these instructions: http://wiki.developers.facebook.com/index.php/Cross-domain_communication_channel
When that file is created, we then add the following code before and after the PHP XFBML. Note, you need to add your API key and change the path to the Cross Domain Channel File in the last line.
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
</head>
<body>
<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" type="text/javascript"></script>
<? //Insert the PHP XFBML and API PHP code above here ?>
<script type="text/javascript">
FB.init("YOUR API KEY HERE", "../fblibrary/xd_receiver.htm"); });
</script>
</body>
</html>
Congrats, you are done!
Here is the full index.php file:
<?php
//Link in library. Note my directory structure may be different
require_once '../fblibrary/facebook.php';
//Authentication Keys
$appapikey = 'YOURAPIKEY';
$appsecret = 'YOURAPISECRET';
//Construct the class
$facebook = new Facebook($appapikey, $appsecret);
//Require login
$user_id = $facebook->require_login();
?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
</head>
<body>
<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" type="text/javascript"></script>
<?
// Greet the currently logged-in user!
echo "<p>Hello, <fb:name uid=\"$user_id\" useyou=\"false\"></fb:name>!</p>";
// Print out at most 25 of the logged-in user's friends, using the friends.get API method
echo "<p>Friends:";
$friends = $facebook->api_client->friends_get();
$friends = array_slice($friends, 0, 25);
foreach ($friends as $friend) {
echo "<br>$friend";
}
echo "</p>";
?>
<script type="text/javascript">
FB.init("YOURAPIKEY", "../fblibrary/xd_receiver.htm");
</script>
</body>
</html>