If you're not using Composer, (why are you not using composer?) then using the new Facebook SDK v4 might seem really hard or even downright impossible.
But fear not! As of version 4.0.9, the SDK now comes with something called an autoloader that will make your life much easier.
The problem: include() hell
There are many examples around the internets that showcase how people have tried to use the SDK by including some 15+ files at the top of their scripts.
require 'src/Facebook/FacebookSession.php';
require 'src/Facebook/FacebookRedirectLoginHelper.php';
require 'src/Facebook/FacebookRequest.php';
require 'src/Facebook/FacebookResponse.php';
require 'src/Facebook/FacebookSDKException.php';
// . . .
These are the files that Composer usually autoloads for you. The problem with this method of including files, other than the fact that it's super ugly, is that if changes are made to internal file structure of the SDK (that that happens often) and you decide to upgrade the SDK at a later date, you'll have to update all your includes to reflect the new file structure. And unless you're an expert on the SDK file structure, that's going to take you all night to fix.
The solution: PSR-4 autoloading
PSR-4 autoloading is amazeballs. If you don't know anything about it, it basically lays out standards for using the spl_autoload_register()
function and namespaces in order to autoload files for you. If implemented properly, you'll only need to include one file for every script.
Normally, Composer handles all this PSR-4 autoloading stuff for you from the vendor/
directory. And it's a dream. All you have to do is include the Composer autoloader file and all the namespaced classes get included for you automatically.
require __DIR__ . '/vendor/autoload.php';
But if you're not using composer, you'll need to implement PSR-4 functionality yourself.
One way to do this would be to copy & paste the awesome PSR-4 autoloading example straight from PHP-FIG and tweak it to work with the SDK. But the good news is that you don't have to because an autoloader was added in 4.0.9.
All you have to do is include the autoloader file called autoload.php
which is included in the root directory of the SDK files and it will add PSR-4 functionality to your script.
require __DIR__ . '/path/to/facebook-php-sdk-v4/autoload.php';
This one line should be able to detect the path to the Facebook SDK source files automatically.
Customizing the include path
But say you've downloaded the SDK source zip file, extracted it, and then moved all the files from src/Facebook/*
to your own custom path. After all, there's no need to copy over the files in the root folder or the tests/
folder if all you care about is the core SDK functionality for your website.
In that case, you'll need to define a constant FACEBOOK_SDK_V4_SRC_DIR
that points to the new source location before you include the autoloader. You'll also need to copy the autoload.php
file from the root directory to wherever you want on your server. You can even rename the autoloader file if you like.
For example, if I moved the files from src/Facebook/*
to facebook-sdk/
on my web server and I moved the autoload.php
file to my root web server, I could specify the new path like so:
define('FACEBOOK_SDK_V4_SRC_DIR', __DIR__ . '/facebook-sdk/');
require __DIR__ . '/autoload.php';
Magically including
Once you've included the autoloader, you can just use the use
keyword to import the namespace and the necessary files will be included for you automatically!
use Facebook\FacebookSession;
FacebookSession::setDefaultApplication('app-id', 'app-secret');
$session = new FacebookSession('access-token');
// . . .
But the moral of this story is... you should be using Composer. :)