Prerequisites: Familiarity with uploading PHP code, file includes and directory structures. If you are uncertain how to do any of these things, I recommend hiring someone to help you.
Installing Facebook’s PHP SDK is really very simple, you have two ways to get the SDK, either via git, a concurrent versioning software package (see definition below) or by downloading the entire SDK as an archive. We’ll use the later for this article.
Facebook is using a site called Github to distribute a large portion of their open source code, which the PHP SDK falls into. You’ll download the PHP SDK to your local machine directly from Github. It’s not necessary to create an account to download the code. Click the “Downloads” button to get started.
Next click, “Download .zip” which will transfer the full SDK to your computer.
Now, open the archive you just downloaded. This should be as simple as double clicking on the file you just downloaded, named something like facebook-php-sdk-v2.1.2-4-g2343fca.zip.
Inside this folder, there are three directories, ./examples, ./src, and ./tests. The ./src directory is the one we are concerned with currently.
Copy the contents of your ./src directory to your application or webserver. If you currently have a directory for php class files or other SDK’s, you’ll want to put these two files there, otherwise, I suggest creating one.
Now you can create a new file that will save you some time later on. This file will create the Facebook object using the application id and secret assigned to you by Facebook when you create your application. Call this file fb_init.php or something similar and save it where you save other includes files on your webserver.
<?php // depending on your hosting provider, you may need to include // the entire path to the directory you added this file to, for example // if your FTP login is, 'tommy', it may be, consult your ISP's docs // for additional assistance: // require_once('/home/tommy/public_html/includes/facebook.php'); require_once('facebook.php'); $facebook = new Facebook(array( 'appId'=>'', // from Facebook 'secret'=>'', // from Facebook 'cookie'=>true )); ?> |
If you need to, or your app is sufficiently small (less than 10 files) you can actually save all of this code a single directory of your website. Just be sure to update your require_once statement to reflect the actual location of the facebook.php file you added to your server.
If you haven’t already done so, you can safely upload/FTP these new files to your webserver and begin working on implementing more advanced features that require the server side PHP SDK. For example, you can create a reveal tab and place content safely behind a required like on Facebook.
Any file you wish to access the Facebook SDK, you can simply include the following code at the top of that file and then begin working with the $facebook object.
include_once('fb_init.php'); |
Concurrent Versioning Software:
Concurrent versioning software is a software program that helps multiple developers work on a single piece of code. It can also be used to distribute the code to others who need to use that software. Common software packages include Git, Subversion (or SVN) and Microsoft Visual Source Safe. Each has their own strengths and weaknesses.