Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cutups's avatar

Package for querying Facebook Graph API

Looking for suggestions about how to handle integrating Facebook Graph API queries into my Laravel app. I have an app that has been on Laravel 6 and was using this package: https://github.com/SammyK/LaravelFacebookSdk

But I recently have started updating to Laravel 8 and it's no longer compatible or maintained. So looking for something that will help handle that.

Right now I get an access token, then query some data from events that users are attending to pull into my app.

0 likes
6 replies
martinbean's avatar

@cutups Facebook has its own PHP SDK: https://github.com/facebookarchive/php-graph-sdk

You can install it through Composer and then just bind a well-configured instance of the Facebook to the container:

use Facebook\Facebook;

class FacebookServiceProvider extends ServiceProvider implements DeferrableProvider
{
    public function register()
    {
        $this->app->singleton(Facebook::class, function () {
            return new Facebook([
                'app_id' => $this->app['config']['services.facebook.client_id'],
                'app_secret' => $this->app['config']['services.facebook.client_secret'],
            ]);
        });
    }

    public function provides()
    {
        return [
            Facebook::class,
        ];
    }
}

You can now type-hint the class in your application (controllers, console commands, queued jobs, etc) and get a well-formed instance:

use Facebook\Facebook;

class FacebookPageController extends Controller
{
    protected $facebook;

    public function __construct(Facebook $facebook)
    {
        $this->facebook = $facebook;
    }
}

By using the official SDK, you’re then not relying on a third-party having to keep their implementation up to date.

2 likes
MarcoTroost's avatar

@martinbean Thank you for the advice and the code snippets. I was facing the same problem as @cutups. Using the official SDK works like a charm!

spyworld's avatar

Create a package instead of integrated singleton behaviour. Creating your own method to handle Facebook API is much better and more control.

For social authorization, Adapter pattern is suitable to store user data.

martinbean's avatar

Why create a package… to work with a package? You can bind the SDK to the container in a service provider, and then create your own classes if you want some “abstraction” between your application’s code the SDK.

Personally, I create repository-like classes, i.e. a FacebookPageRepository class, that is injected with the SDK instance bound in the container, does the API request using the SDK instance, and returns a collection of pages the user is authorised to view:

use App\Services\Facebook\Repositories\FacebookPageRepository;

class FacebookPageController extends Controller
{
    protected $facebookPages;

    public function __construct(FacebookPageRepository $facebookPages)
    {
        $this->facebookPages = $facebookPages;
    }

    public function index()
    {
        $pages = $this->facebookPages->all();

        // Return view
    }
}

For social authorisation, there’s Socialite and the Facebook adapter:

return Socialite::driver('facebook')->redirect();
$facebookUser = Socialite::driver('facebook')->user();
1 like
spyworld's avatar

Creating a package with factory method pattern as abstract to force subclasses to implement their own method. Just like socialite package.

Please or to participate in this conversation.