Skip to content

Instantly share code, notes, and snippets.

@saimonh3
Created December 13, 2016 09:01
Show Gist options
  • Select an option

  • Save saimonh3/30b01ef39ccedea6d8f7c2fdd4233f66 to your computer and use it in GitHub Desktop.

Select an option

Save saimonh3/30b01ef39ccedea6d8f7c2fdd4233f66 to your computer and use it in GitHub Desktop.
Getting User info using Facebook API.
<?php
session_start();
require_once __DIR__ . '/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'APP_ID',
'app_secret' => 'APP_SECRET',
'default_graph_version' => 'v2.5',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['user_birthday', 'user_location', 'user_website']; // optional
try {
if (isset($_SESSION['localhost_app_token'])) {
$accessToken = $_SESSION['localhost_app_token'];
} else {
$accessToken = $helper->getAccessToken();
}
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (isset($accessToken)) {
if (isset($_SESSION['localhost_app_token'])) {
$fb->setDefaultAccessToken($_SESSION['localhost_app_token']);
} else {
// getting short-lived access token
$_SESSION['localhost_app_token'] = (string) $accessToken;
// OAuth 2.0 client handler
$oAuth2Client = $fb->getOAuth2Client();
// Exchanges a short-lived access token for a long-lived one
$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['localhost_app_token']);
$_SESSION['localhost_app_token'] = (string) $longLivedAccessToken;
// setting default access token to be used in script
$fb->setDefaultAccessToken($_SESSION['localhost_app_token']);
}
// redirect the user back to the same page if it has "code" GET variable
if (isset($_GET['code'])) {
header('Location: ./');
}
// getting basic info about user
try {
$profile_request = $fb->get('/me?fields=name,first_name,last_name,birthday,website,location');
$profile = $profile_request->getGraphNode()->asArray();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
session_destroy();
// redirecting user back to app login page
header("Location: ./");
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
// printing $profile array on the screen which holds the basic info about user
echo $profile['birthday']->format('d-m-Y');
echo $profile['website'];
echo $profile['location']['name'];
// Now you can redirect to another page and use the access token from $_SESSION['localhost_app_token']
} else {
// replace your website URL same as added in the developers.facebook.com/apps e.g. if you used http instead of https and you used non-www version or www version of your website then you must add the same here
$loginUrl = $helper->getLoginUrl('http://sohaibilyas.com/fbapp/', $permissions);
echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment