Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

adrian7/facebook-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Devlib/Facebook-API

Simple Facebook API wrapper for PHP

Install

composer require devlib/facebook-api

Usage

use \DevLib\API\Facebook\App;

$appId     = getenv('FACEBOOK_APP_ID');
$appSecret = getenv('FACEBOOK_APP_SECRET');

$permissions = ['email', 'user_posts'];
$callback    = ('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . '?callback=1');

$app         = App::create($appId, $appSecret)
                   ->withPermissions($permissions)
                   ->withCallbackURL($callback);

Generate login url

//display link for authentication
echo '<a href="' . $app->getLoginURL() . '">Login with Facebook</a>';

Retrieve user info

try{

    $user = $app->getUser();
    $data = $user->get(['id', 'name', 'email'])->getGraphUser();

    //successful log in
    echo ( '<h3><i>#' . $data->getId() . '</i> ' . $data->getEmail() );

}
catch (\Facebook\Exceptions\FacebookAuthorizationException $e){
    echo ('Error: ' . $e->getMessage() );
}

Facebook API Reference: