Whether you’re developing a website with Facebook login, creating a Facebook Canvas app or Page tab, the Facebook SDK for PHP does all the heavy lifting for you making it as easy as possible to deeply integrate into the Facebook platform.
The Facebook SDK for PHP provides a native interface to the Graph API and Facebook Login.
PHP SDK Basics
The Facebook SDK for PHP is a library with powerful features that enable PHP developers to easily integrate Facebook login and make requests to the Graph API. It also plays well with the Facebook SDK for JavaScript to give the front-end user the best possible user experience. But it doesn’t end there, the Facebook SDK for PHP makes it easy to upload photos and videos and send batch requests to the Graph API among other things. And SDK for PHP has many extensibility points giving PHP developers full control of how the SDK for PHP interacts with their specific hosting environment and web framework.
Whether you’re developing a website with Facebook login, creating a Facebook Canvas app or Page tab, the Facebook SDK for PHP does all the heavy lifting for you making it as easy as possible to deeply integrate into the Facebook platform.
Autoloading & namespaces
The Facebook SDK for PHP v5 is coded in compliance with PSR-4. This means it relies heavily on namespaces so that class files can be loaded for you automatically.
It would be advantageous to familiarize yourself with the concepts of namespacing and autoloading if you are not already acquainted with them.
There are two methods to install the Facebook SDK for PHP. The recommended installation method is by using Composer. If are unable to use Composer for your project, you can still install the SDK manually by downloading the source files and including the autoloader.
System requirements
- PHP 5.4 or greater
- Composer (optional) – Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.
Installing with Composer
The Facebook SDK starting adhering to SemVer with version 5. Previous to version 5, the SDK did not follow SemVer. Composer is the recommended way to install the Facebook SDK for PHP. Simply run the following in the root of your project.
composer require facebook/graph-sdk
Once you do this, composer will edit your composer.json file and download the latest version of the SDK and put it in the /vendor/ directory.
Make sure to include the Composer autoloader at the top of your script.
require_once __DIR__ . ‘/vendor/autoload.php’;
Manually Installation
First, download the source code and unzip it wherever you like in your project.
Download the SDK for PHP v5 at link – https://github.com/facebook/php-graph-sdk/archive/5.4.zip
Then include the autoloader provided in the SDK at the top of your script.
require_once __DIR__ . ‘/path/to/php-graph-sdk/src/Facebook/autoload.php’;
The autoloader should be able to auto-detect the proper location of the source code.
The source code includes myriad files that aren’t necessary for use in a production environment. If you’d like to strip out everything except the core files, follow this example. For this example we’ll assume the root of your website is /var/html.
After downloading the source code with the button above, extract the files in a temporary directory.
Move the folder src/Facebook to the root of your website installation or where ever you like to put third-party code. For this example we’ll rename the Facebook directory to facebook-sdk-v5.
The path the core SDK files should now be located in /var/html/facebook-sdk-v5 and inside will also be the autoload.php file.
Assuming we have a script called index.php in the root of our web project, we need to include the autoloader at the top of our script.
require_once __DIR__ . ‘/facebook-sdk-v5/autoload.php’;
If the autoloader is having trouble detecting the path to the source files, we can define the location of the source code before the require_once statement.
define(‘FACEBOOK_SDK_V4_SRC_DIR’, __DIR__ . ‘/facebook-sdk-v5/’);
require_once __DIR__ . ‘/facebook-sdk-v5/autoload.php’;