PHP code example of simplon / facebook

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

    

simplon / facebook example snippets


$appId = 'YOUR-APPID';
$appSecret = 'YOUR-APPSECRET';

// get a facebook app instance
$app = new FacebookApps($appId, $appSecret);

// we need a user instance
$user = new FacebookUsers($app, new FacebookPosts());

// callback url
$urlCallback = 'https://your-domain.com/callback/';

// we will fly with the default permissions: public_profile
$permissions = [];

// now lets build the url
$user->getUrlAuthentication($urlCallback, $permissions); // https://www.facebook.com/dialog/oauth?client_id=...

// redirect user to the given url...

$appId = 'YOUR-APPID';
$appSecret = 'YOUR-APPSECRET';

// get a facebook app instance
$app = new FacebookApps($appId, $appSecret);

// we need a user instance
$user = new FacebookUsers($app, new FacebookPosts());

$code = 'AQBggnsv...'; // received code
$urlCallback = 'https://your-domain.com/callback/'; // this needs to be the exact same url as before

// request access token
$user->requestAccessTokenByCode($code, $urlCallback)

// print token
echo $user->getAccessToken(); // CAANc8o7bTTcBAPyTZCwW....

$appId = 'YOUR-APPID';
$appSecret = 'YOUR-APPSECRET';

// your saved access token
$userAccessToken = 'CAANc8o7bTTcBAPyTZCwW....';

// get a facebook app instance
$app = new FacebookApps($appId, $appSecret);

// we need an app access token for this
$app->requestAccessToken();

// now we can have a look at the attached data for the user access token
$debugTokenVo = $app->getDebugTokenVo($userAccessToken); // have a look at the class DebugTokenData

// is this a short term token?
$debugTokenVo->isShortTermToken(); // bool

$appId = 'YOUR-APPID';
$appSecret = 'YOUR-APPSECRET';

// your saved access token
$userAccessToken = 'CAANc8o7bTTcBAPyTZCwW....';

// get a facebook app instance
$app = new FacebookApps($appId, $appSecret);

// we need a user instance
$user = new FacebookUsers($app, new FacebookPosts());

// request long term token
$user->setAccessToken($userAccessToken)->requestLongTermAccessToken();

// your new token
$user->getAccessToken(); // save the new token away

$appId = 'YOUR-APPID';
$appSecret = 'YOUR-APPSECRET';

// your saved access token
$userAccessToken = 'CAANc8o7bTTcBAPyTZCwW....';

// get a facebook app instance
$app = new FacebookApps($appId, $appSecret);

// we need a user instance
$user = new FacebookUsers($app, new FacebookPosts());

// fetch user data
$facebookUserDataVo = $user->setAccessToken($userAccessToken)->getUserData(); // the class holds all data

// lets print the name
$facebookUserDataVo->getFullName(); // Foo Bar

YOUR_CALLBACK_URL?
  error_reason=user_denied
  &error=access_denied
  &error_description=The+user+denied+your+request.