PHP code example of devlib / facebook-api

1. Go to this page and download the library: Download devlib/facebook-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

devlib / facebook-api example snippets


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() );
}