<?php
session_start();
require './config/facebook.php';
require './compose/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']);
$helpder = new FacebookRedirectLoginHelper('http://localhost/start.php');
try {
$session = $helpder->getSessionFromRedirect();
if ($session):
$_SESSION['facebook'] = $session->getToken();
header('Location: index.php');
endif;
if (isset($_SESSION['facebook'])):
$session = new FacebookSession($_SESSION['facebook']);
$request = new FacebookRequest($session, 'GET', '/me');
$response = $request->execute();
$graphObjectClass = $response->getGraphObject(GraphUser::className());
$name = $graphObjectClass->getName();
$id = $graphObjectClass->getId();
$image = 'https://graph.facebook.com/' . $id . '/picture?width=3000&&height=3000';
$mysqli = new mysqli($host, $user, $pass, $bd);
$query = $mysqli->query("SELECT facebook_id FROM usuario WHERE facebook_id='id'");
if ($query->num_rows > 0) {
//El usuario ya existe
} else {
//El usuario no existe
$facebook_id = $graphObjectClass->getId();
$name = $graphObjectClass->getName();
$email = $graphObjectClass->getEmail(['email']);
//Insertamos en la BD
$insert_user = $mysqli->query("INSERT INTO usario (username,email,faccebook_id) VALUES ('$name','$email','$facebook_id')");
}
endif;
} catch (Exception $exc) {
}