Basic Skeleton for using the Facebook PHP SDK v.4.4.0

 

<? session_start();

require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );

use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;

// init app with app id (APPID) and secret (SECRET)
$appID = 'YOUR_APP_ID';
$appSecret = 'YOUR_APP_SECRET';
$appLoginURL = 'https://YOUR_APP_LOGIN_URL';
$appLogoutURL = 'https://YOUR_APP_LOGOUT_URL';

FacebookSession
::setDefaultApplication($appID, $appSecret);

$helper = new FacebookRedirectLoginHelper($appLoginURL ); // login helper with redirect_uri

try
{
$session = $helper->getSessionFromRedirect();
}
catch( FacebookRequestException $ex )
{
// When Facebook returns an error
}
catch( Exception $ex )
{
// When validation fails or other local issues
}


if ( isset( $session ) ) // see if we have a session
{
$request = new FacebookRequest( $session, 'GET', '/me' ); // graph api request for user data
$response = $request->execute();

$graphObject = $response->getGraphObject(); // get response
$graphObject_keys = array("id", "email", "first_name", "last_name", "gender", "link", "locale", "name", "timezone", "updated_time", "verified");
for ($i=0; $i<count($graphObject_keys); $i++){
echo $graphObject_keys[$i] . " : " . $graphObject->getProperty($graphObject_keys[$i]) . "<br/>";
}

$logoutURL = $appLogoutURL . '">Logout</a>';
echo '<a href="' . $helper->getLogoutUrl( $session, $logoutURL);
}
//end if
else
{
echo '<a href="' . $helper->getLoginUrl() . '">Login</a>'; // show login url
} //end else

?>

Comments

Post a Comment

Popular posts from this blog

How to create an organizational chart in your webpage using Google Organization Chart Tools

Embed JSX code with if condition in React app