rodrigobertin
5/8/2015 - 12:19 AM

login con facebook

login con facebook

<?php
$config = array(
  'app_id' => '1615488025331762',
  'app_secret' => '53c6cd9d51cfe8e4105517f999e9bd42',
  'scopes' => array('scope' => 'email,public_profile,user_location')
);

session_start();
require 'vendor/autoload.php';

use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\GraphUser;
use Facebook\GraphObject;
use Facebook\FacebookRequestException;

FacebookSession::setDefaultApplication($config['app_id'], $config['app_secret']);
$helper = new FacebookRedirectLoginHelper(url);

try {
  $session = $helper->getSessionFromRedirect();

  if ($session){
    $_SESSION['facebook'] = $session->getToken();
    header('Location: index.php');
  }

  if (isset($_SESSION['facebook'])){
    $session = new FacebookSession($_SESSION['facebook']);
    $request = new FacebookRequest($session, 'GET', '/me');
    $response = $request->execute();
    $graphObjectClass = $response->getGraphObject(GraphUser::className());
    $facebook_user = $graphObjectClass;
  }


} catch(FacebookRequestException $ex) {
  // When Facebook returns an error
  echo $ex;
} catch(\Exception $ex) {
  // When validation fails or other local issues
  echo $ex;
}

?>