sabato 18 maggio 2013

Login with Facebook Php

Login with Facebook via PHP has never been so easy!The integration between Facebook and Php is often synonymous with Rogne and various problems.I will try to make you understand in the way more simple as possible how to achieve a page php which carries out the user's login on facebook and pulls out the credentials storing them into variables.
First of all download the SDK for PHP from here: https://github.com/facebook/facebook-php-sdk;Now create a file Accedi.php and copy the code below:
<?

//richiamo la facebook php sdk
require 'facebook-php-sdk-master/src/facebook.php';

//mi collego a facebook, sostituisco i parametri AppID e AppSecret con quelli della mia App
$app_id = 'xxxxxxxxxx';
$app_secret = 'xxxxxxxxx';
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));

// Verifico se l'utente è connesso altrimenti lo collego
$user = $facebook->getUser();

//Controlla se l'utente è loggato su Facebook
if ($user) {
try {
   
// Cose da fare se l'utente è connesso
// Stampo informazioni dell'utente

$user_profile = $facebook->api('/me');

$first = $user_profile['first_name'];
echo "Benvenuto ";
echo $first;
echo " ";
$last = $user_profile['last_name'];
echo $last;
echo "<p>";

$FB_ID = $user_profile['id'];
echo "Il tuo Facebook ID e': ";
echo $FB_ID;
echo "<p>";

$FB_LINK = $user_profile['link'];
echo "Il tuo Facebook Link e': ";
echo $FB_LINK;
echo "<p>";

$compleanno = $user_profile['birthday'];
echo "Sei nato il: ";

$separatore="/";
$suddivisa=explode($separatore, $compleanno);
$mese=$suddivisa[0];
$giorno=$suddivisa[1];
$anno=$suddivisa[2];

echo $giorno;
echo"/";
echo $mese;
echo "/";
echo $anno;
echo "<p>";

$citta = $user_profile['location']['name'];

echo "Vivi a : ";
echo $citta;
echo "<p>";

$cittanatale = $user_profile['hometown']['name'];
if($cittanatale){
echo "Sei nato a : ";
echo $cittanatale;
echo "<p>";
}
$istruzione = $user_profile['education'][0]['school']['name'];
if($istruzione){
echo "Istruzione : ";
echo $istruzione;
echo "<p>";
}

$sesso = $user_profile['gender'];
echo "Sei: ";
if($sesso=="male"){
    $sesso="Maschio";
}
else if($sesso=="female"){
    $sesso="Femmina";
}
echo $sesso;
echo "<p>";

$lingua = $user_profile['locale'];
echo "Lingua: ";
$intermezzo="_";
$tokenlingua=explode($intermezzo, $lingua);
$lingua=$tokenlingua[1];
echo $lingua;
echo "<p>";

echo "<img src='http://graph.facebook.com/$FB_ID/picture'/>";

echo "<p>";

} catch (FacebookApiException $e) {
   
//se avviene qualche errore lo segnalo
error_log($e);
$user = null;
}
} else {
   
//se l'utente non è loggato faccio fare il login automatico
$params = array(
'scope' => 'user_birthday',
);
$loginUrl = $facebook->getLoginUrl($params);
echo("<script> top.location.href='" . $loginUrl . "'</script>");
}
?>

 
Replace the app id and app secret with those of your facebook app, copy the folder the SDK in the same directory where the file is located and you will Accedi.php concluded.When you open the page Accedi.php will ask you to log in with facebook and will print your personal data!

Nessun commento:

Posta un commento