Simple Facebook API wrapper for PHP
composer require devlib/facebook-api
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);
//display link for authentication
echo '<a href="' . $app->getLoginURL() . '">Login with Facebook</a>';
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() );
}