Skip to content

Instantly share code, notes, and snippets.

@REPTILEHAUS
Last active February 26, 2017 18:59
Show Gist options
  • Save REPTILEHAUS/9f33559bc8db930c4abfc24723f92e7d to your computer and use it in GitHub Desktop.
Save REPTILEHAUS/9f33559bc8db930c4abfc24723f92e7d to your computer and use it in GitHub Desktop.
<?php
session_start();
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
define('APP_ID', '337348378287384');
define('APP_SECRET', '83d7gd238r74r8w7827565863864fds8');
define('REDIRECT_URL', 'http://yoursite.com/test.php');
require_once “vendor/autoload.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;
use Facebook\Entities\AccessToken;
// And finally we will add all the login for setting our default application, initializing the Facebook helper class, error exception block and then we will process the data received from Facebook via the class method getProperty, take note that to get the users image we use the users ID and a special Facebook graph URL that ends with picture?type=square. From this you can do a multitude of things which I have not covered here like write this info to a database which would be pretty simple to do if you assign the getProperty to variables and then just do a simple MySQL INSERT and to save the users photo to the server in some location you could use the PHP function file_get_contents.
FacebookSession::setDefaultApplication( APP_ID, APP_SECRET );
$helper = new FacebookRedirectLoginHelper( REDIRECT_URL );
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 ) ) {
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
$graphObject = $response->getGraphObject();
printf("name is %s and your email is %s. Your name is %s %s. ", $graphObject->getProperty('name'), $graphObject->getProperty('email'), $graphObject->getProperty('first_name'), $graphObject->getProperty('last_name') );
echo "link =" . $graphObject->getProperty('link') . "<br>";
echo "image =" . $graphObject->getProperty('image') . "<br>";
echo '<img src="https://graph.facebook.com/'. $graphObject->getProperty('id') .'/picture?type=square" />';
} else {
echo '<a href="' . $helper->getLoginUrl( array('scope' => 'email, user_birthday, user_location, user_work_history, user_about_me, user_hometown')) . '">Login</a>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment