Jednoduchý příklad přihlašování prostřednictvím FB na cizích stránkách.
- V první řadě se musí založit v developerském účtu FB nová aplikace.
- Z této aplikace získáme App ID a App Secret, které vložíme do níže uvedeného kódu.
<?php
session_start();
//Include Facebook SDK
require_once 'inc/facebook.php';
/*
* Configuration and setup FB API
*/
$appId = 'APPID'; //Facebook App ID
$appSecret = 'APPSECRET'; // Facebook App Secret
$redirectURL = 'http://YOURURL/fblogin/index.php'; // Callback URL
$fbPermissions = 'email'; //Required facebook permissions
//Call Facebook API
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret
));
if($_GET['action']=='logout'){
$facebook->destroySession();
}
$fbUser = $facebook->getUser();
if(!$fbUser){
$fbUser = NULL;
$loginURL = $facebook->getLoginUrl(array('redirect_uri'=>$redirectURL,'scope'=>$fbPermissions));
$output = '<a class="btn btn-lg btn-social foursquare btn-facebook" href='.$loginURL.'>
<span class="fa fa-facebook"></span> Sign in with Facebook
</a>';
}else{
//Get user profile data from facebook
$fbUserProfile = $facebook->api('/me?fields=id,first_name,last_name,email,link,gender,locale,picture');
//Initialize User class
//$user = new User();
//Insert or update user data to the database
$fbUserData = array(
'oauth_provider'=> 'facebook',
'oauth_uid' => $fbUserProfile['id'],
'first_name' => $fbUserProfile['first_name'],
'last_name' => $fbUserProfile['last_name'],
'email' => $fbUserProfile['email'],
'gender' => $fbUserProfile['gender'],
'locale' => $fbUserProfile['locale'],
'picture' => $fbUserProfile['picture']['data']['url'],
'link' => $fbUserProfile['link']
);
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login with Facebook using PHP</title>
<style type="text/css">
h1{font-family:Arial, Helvetica, sans-serif;color:#999999;}
</style>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container" style="margin-top: 30px;">
<?php
if(!empty($fbUserData)){
?>
<h2>Facebook Profile Details | VOXCAFE</h2>
<table class="table table-hover">
<tr>
<th>Profile Image</th>
<th>Facebook ID</th>
<th>Fullname</th>
<th>Email</th>
<th>Gender</th>
<th>locale</th>
<th>Visit Facebook</th>
<th>Logout</th>
</tr>
<tr>
<td><img src="<?php echo $fbUserData['picture']; ?>" class="img img-circle"></td>
<td><?php echo $fbUserData['oauth_uid']; ?></td>
<td><?php echo $fbUserData['first_name'].' '.$fbUserData['last_name']; ?></td>
<td><?php echo $fbUserData['email']; ?></td>
<td><?php echo $fbUserData['gender']; ?></td>
<td><?php echo $fbUserData['locale']; ?></td>
<td><a href="<?php echo $fbUserData['link']; ?>">Visit Facebook Link</a></td>
<td><a href="index.php?action=logout">Logout</a></td>
</tr>
</table>
<?php
}else{
echo $output;
}
?>
</div>
</body>
</html>
Změňte konstanty App Id, App Secret a rediretURL.
Důležité je, aby doména v nastavení aplikace v developerském účtu musí být stejná jako v proměnné redirectURL.
Dále musí být provedeny úpravy v originálních souborech knihovny od FB. V souboru base_facebook.php se musí změnit:
public function getAccessToken() {
if ($this->accessToken !== null) {
// we've done this already and cached it. Just return.
return $this->accessToken;
}
// first establish access token to be the application
// access token, in case we navigate to the /oauth/access_token
// endpoint, where SOME access token is required.
$getApplicationAccessToken = $this->getApplicationAccessToken();
$this->setAccessToken($getApplicationAccessToken);
$user_access_token = $this->getUserAccessToken();
if ($user_access_token) {
$this->setAccessToken($user_access_token);
}
return $this->accessToken;
}
na
public function getAccessToken() {
if ($this->accessToken !== null) {
// we've done this already and cached it. Just return.
return $this->accessToken;
}
// first establish access token to be the application
// access token, in case we navigate to the /oauth/access_token
// endpoint, where SOME access token is required.
$getApplicationAccessToken = $this->getApplicationAccessToken();
$this->setAccessToken($getApplicationAccessToken);
$user_access_token = $this->getUserAccessToken();
if ($user_access_token) {
//$this->setAccessToken($user_access_token);
$this->accessToken = $user_access_token; //edit; msolution
}
return $this->accessToken;
}
A dále ve funkci getAccessTokenFromCode() změnit
parse_str($access_token_response, $response_params);
na
//parse_str($access_token_response, $response_params); //edit:: msolution;;
$response_params = json_decode($access_token_response, true );
V přiložených souborech je již vše opravené a funkční.