PHP code example of pitchanon / facebook-connect

1. Go to this page and download the library: Download pitchanon/facebook-connect 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/ */

    

pitchanon / facebook-connect example snippets


use FacebookConnect;

Route::get('/auth/facebook', function () {
    $state = bin2hex(random_bytes(16));
    session(['fb_oauth_state' => $state]);

    return redirect(FacebookConnect::getLoginUrl(
        redirectUri: null, // falls back to config
        scopes: ['email', 'public_profile'],
        state: $state,
    ));
});

Route::get('/auth/facebook/callback', function (\Illuminate\Http\Request $request) {
    abort_unless(hash_equals(session('fb_oauth_state', ''), (string) $request->query('state')), 419);

    $token = FacebookConnect::getAccessTokenFromCode($request->query('code'));
    $longLived = FacebookConnect::getLongLivedToken($token['access_token']);

    $profile = FacebookConnect::getUser($longLived['access_token'], ['id', 'name', 'email']);

    // persist $profile + $longLived['access_token'] however you like
    return $profile;
});

FacebookConnect::postToFeed([
    'message' => 'Hello from Laravel!',
    'link'    => 'https://example.com',
], $accessToken, target: 'me');

$likes = FacebookConnect::userLikesPage($userId, $pageId, $accessToken);

if (!FacebookConnect::hasGrantedPermissions($accessToken, ['email'])) {
    return redirect(FacebookConnect::getLoginUrl());
}

FacebookConnect::get('/me/photos', ['access_token' => $accessToken, 'limit' => 5]);
FacebookConnect::post('/me/feed', ['message' => 'Hi', 'access_token' => $accessToken]);
bash
php artisan vendor:publish --tag=facebook-connect-config