Laravel Facebook Graph API

Packages

April 1st, 2022

Laravel Facebook Graph API

Laravel Facebook Graph by Joel Butcher is a Laravel integration for the Graph PHP 8 SDK project. This package makes it easy to integrate the PHP SDK (provided by the same author) and Facebook login into your Laravel projects.

After you install this package and configure your app, you can use the provided Facebook facade/service to access the Facebook API:

// Facade
Facebook::getUser(array $params);
 
// Service container making a GET request
app(\JoelButcher\Facebook\Facebook::class)->get('/me', array $params);

Here's an example from the underlying PHP 8 SDK to get a user name and ID:

$fb = new Facebook\Facebook([
'app_id' => '{app-id}',
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.10',
]);
 
try {
// Returns a `Facebook\Response` object
$response = $fb->get('/me?fields=id,name', '{access-token}');
} catch(Facebook\Exception\ResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exception\SDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
 
$user = $response->getGraphUser();
 
// Array access
echo 'Name: ' . $user['name'];
// or get
echo 'Name: ' . $user->getName();

Learn More

You can get started with this package by reading the documentation for the Graph PHP 8 SDK used by the Laravel wrapper. It includes plenty of examples.

To use this project in a Laravel app, check out joelbutcher/laravel-facebook-graph. Also, check out the PHP SDK used by the Laravel package.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.