Getting started with the Facebook SDK for PHP
The Facebook SDK for PHP provides developers with a modern, native library for accessing the Graph API and taking advantage of Facebook Login. Usually this means you're developing with PHP for a Facebook Canvas app, building your own website, or adding server-side functionality to an app that already uses the Facebook SDK for JavaScript.
Download the SDK
The Facebook SDK for PHP v4 requires PHP 5.4 or greater.
If you're using Composer as a package manager for PHP, which we recommend, installing the SDK is as easy as adding a require entry for the Facebook SDK for PHP to the
composer.json
file in the root of your project:{
"require" : {
"facebook/php-sdk-v4" : "4.0.*"
}
}
Then run composer with the install parameter, and it will download the newest version. If you're using the autoloader as part of Composer, the Facebook namespace will be available for use without adding require statements for all of the files.
Loading the SDK without Composer
If you're not using Composer, you can download the SDK from our GitHub:
Download the PHP SDK
Load the SDK like this:
define('FACEBOOK_SDK_V4_SRC_DIR', '/path/to/fb-php-sdk-v4/src/Facebook/');
require __DIR__ . '/path/to/facebook-php-sdk-v4/autoload.php';
Initializing
You will need to have configured a Facebook App, which you can obtain from the App Dashboard.
Then, initialize the SDK with your app ID and secret:
// Make sure to load the Facebook SDK for PHP via composer or manually
use Facebook\FacebookSession;
// add other classes you plan to use, e.g.:
// use Facebook\FacebookRequest;
// use Facebook\GraphUser;
// use Facebook\FacebookRequestException;
FacebookSession::setDefaultApplication('YOUR_APP_ID', 'YOUR_APP_SECRET');
Authentication and authorization
The SDK can be used to support login your site using a Facebook account. On the server-side, the SDK provides helper classes for the most common scenarios.
For most websites, you'll use the FacebookRedirectLoginHelper. Generate the login URL to redirect visitors to with the
getLoginUrl()
method, redirect them, and then process the response from Facebook with the getSessionFromRedirect()
method, which returns a FacebookSession
.// Add `use Facebook\FacebookRedirectLoginHelper;` to top of file
$helper = new FacebookRedirectLoginHelper('your redirect URL here');
$loginUrl = $helper->getLoginUrl();
// Use the login url on a link or button to
// redirect to Facebook for authentication
// Add `use Facebook\FacebookRedirectLoginHelper;` to top of file
$helper = new FacebookRedirectLoginHelper();
try {
$session = $helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {
// When Facebook returns an error
} catch(\Exception $ex) {
// When validation fails or other local issues
}
if ($session) {
// Logged in
}
If your app is on Facebook Canvas, use the
getSession()
method on FacebookCanvasLoginHelperto get a FacebookSession
for the user.// Add `use Facebook\FacebookCanvasLoginHelper;` to top of file
$helper = new FacebookCanvasLoginHelper();
try {
$session = $helper->getSession();
} catch(FacebookRequestException $ex) {
// When Facebook returns an error
} catch(\Exception $ex) {
// When validation fails or other local issues
}
if ($session) {
// Logged in
}
If you're already using the Facebook SDK for JavaScript to authenticate users, you can also log them in on the server side with the FacebookJavaScriptLoginHelper. The
getSession()
method will return aFacebookSession
.// Add `use Facebook\FacebookJavaScriptLoginHelper;` to top of file
$helper = new FacebookJavaScriptLoginHelper();
try {
$session = $helper->getSession();
} catch(FacebookRequestException $ex) {
// When Facebook returns an error
} catch(\Exception $ex) {
// When validation fails or other local issues
}
if ($session) {
// Logged in
}
You can also create a
FacebookSession
with an access token you've acquired through some other means, by passing it to the constructor.// Add `use Facebook\FacebookSession;` to top of file
$session = new FacebookSession('access token here');
Making Requests to the Graph API
Once you have a
FacebookSession
you can begin making calls to the Graph API withFacebookRequest.// Add `use Facebook\FacebookRequest;` to top of file
$request = new FacebookRequest($session, 'GET', '/me');
$response = $request->execute();
$graphObject = $response->getGraphObject();
You can also chain these methods:
$me = (new FacebookRequest(
$session, 'GET', '/me'
))->execute()->getGraphObject(GraphUser::className());
For more details, see the examples and API reference for all of these classes listed on the landing page for the Facebook SDK for PHP.
Comments
Post a Comment