Laravel Facebook Graph API

Packages

April 1st, 2022

laravel-facebook.png

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:

1// Facade
2Facebook::getUser(array $params);
3 
4// Service container making a GET request
5app(\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:

1$fb = new Facebook\Facebook([
2 'app_id' => '{app-id}',
3 'app_secret' => '{app-secret}',
4 'default_graph_version' => 'v2.10',
5 ]);
6 
7try {
8 // Returns a `Facebook\Response` object
9 $response = $fb->get('/me?fields=id,name', '{access-token}');
10} catch(Facebook\Exception\ResponseException $e) {
11 echo 'Graph returned an error: ' . $e->getMessage();
12 exit;
13} catch(Facebook\Exception\SDKException $e) {
14 echo 'Facebook SDK returned an error: ' . $e->getMessage();
15 exit;
16}
17 
18$user = $response->getGraphUser();
19 
20// Array access
21echo 'Name: ' . $user['name'];
22// or get
23echo '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.

Laravel News Partners