Get Started Integrating Facebook with the PHP-SDK

The FaceBook PHP-SDK simplifies some of the more complex tasks - like OAuth authentication - needed to integrate your website or application with Facebook. This article will illustrate how to register your application and install the PHP-SDK on any PHP supported Hosting.com Platform.  After installing the PHP-SDK you will qbickly be able to build integrated features. For example, you can allow visitors to log into your site with their FaceBook account or sign up for a service you providing using a form pre-populated with their Facebook information.

Obtain a Facebook Application ID and Secret

Go to http://www.facebook.com/developers/ and log into Facebook.

Click on the +New Application to register your application.

step1

If your account is not verified you will need to do that at this time. Click on the mobile phone link and you will receive a text message that contains a confirmation code. Enter the confirmation code into the box and click Confirm.

verifyAccount

Enter and App Name Agree to the Terms and click Create App.

Step2

Enter the captcha.

Step3

On the About tab provide basic information about your website/application.

Step4

Click Save Changes at the bottom. On the next page you will be provided with your App ID, API Key and App Secret.  You will need this information for the PHP-SDK.

You will also be provided with a link for Example Code. This link provides instructions to download and install the PHP-SDK.

settings

Click on the Web Site tab and enter the url to your application and your domain name.

website

Install the Facebook PHP SDK

Step 1: Download Facebook’s PHP Library

From SSH: Extract this archive into a directory on your hosting server where you can host and run PHP code.

$ curl -L http://github.com/facebook/php-sdk/tarball/master | tar xvz
$ mv facebook-php-sdk-* facebook-php-sdk
$ cp facebook-php-sdk/examples/example.php index.php

If you are not comfortable with SSH you can download it from https://github.com/facebook/php-sdk/. Click the Downloads button, unpack the files and upload them to your hosting server.  Make a copy of examples/example.php and name it index.php.

Step 2: Replace the IDs in index.php to have your own app information

<?php
// Awesome Facebook Application
//
// Name: Your app name
//
require_once 'facebook-php-sdk/src/facebook.php';
// Create our Application instance.
$facebook = new Facebook(array(
  'appId' => '123456789',
  'secret' => '222222fffffffffffeeeeeeeeeee333333333',
  'cookie' => true,
));

 

Browse to your http://yoursite.com/examples/index.php to test it outside of Facebook.

Step 3: Test Your Application on FaceBook

To test your application on Facebook you need to provide two more pieces of information to Facebook.  Edit your application and click on the Facebook integration tab. 

At a minimum enter values in the Canvas Page and URL fields.   

FacebookIntegration

Once you have provided your Cavas Page and URL you should be able to browser to http://apps.facebook.com/yourapp/ to see your site load.