我正在使用以下代码将我的 facebook 应用程序用户详细信息存储在 mysql 中,以允许他们在我的网站上查看独家内容。
我的问题很简单,如何将用户的访问令牌存储在我的 Mysql 表中,以便以后使用它来发布到墙上
<?php
session_start();
if(isset($_POST["connect"]) && $_POST["connect"]==1)
{
include_once("config.php"); //Include configuration file.
//Call Facebook API
if (!class_exists('FacebookApiException')) {
require_once('inc/facebook.php' );
}
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret,
));
$fbuser = $facebook->getUser();
if ($fbuser) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$me = $facebook->api('/me'); //user
$uid = $facebook->getUser();
}
catch (FacebookApiException $e)
{
//echo error_log($e);
$fbuser = null;
}
}
// redirect user to facebook login page if empty data or fresh login requires
if (!$fbuser){
$loginUrl = $facebook->getLoginUrl(array('redirect_uri'=>$return_url, false));
header('Location: '.$loginUrl);
}
//user details
$fullname = $me['first_name'].' '.$me['last_name'];
$email = $me['email'];
$username = $me['username'];
$gender = $me['gender'];
$locale = $me['locale'];
$token = $me['token'];
/* connect to mysql */
$connecDB = mysql_connect($hostname, $db_username, $db_password)or die("Unable to connect to MySQL");
mysql_select_db($db_name,$connecDB);
//Check user id in our database
$result = mysql_query("SELECT COUNT(id) FROM usertable WHERE fbid=$uid");
$UserCount = mysql_fetch_array($result);
if($UserCount[0])
{
//User exist, Show welcome back message
echo 'Ajax Response :<br /><strong>Welcome back '. $me['first_name'] . ' '. $me['last_name'].'!</strong> AT : '.$token.' ( Facebook ID : '.$uid.') [<a href="'.$return_url.'?logout=1">Log Ouuuuuuuuuuuuuut</a>]';
echo '<iframe style="width:1309px; height:526px" src="post2.php"></iframe>';
//User is now connected, log him in
login_user(true,$me['first_name'].' '.$me['last_name']);
}
else
{
//User is new, Show connected message and store info in our Database
// Insert user into Database.
@mysql_query("INSERT INTO usertable (fbid, fullname, email, username, gender, token, locale) VALUES ($uid, '$fullname','$email','$username','$gender','$token','$locale')");
//User is now connected, log him in
login_user(true,$me['first_name'].' '.$me['last_name']);
}
mysql_close($connecDB);
}
function login_user($loggedin,$user_name)
{
/*
function stores some session variables to imitate user login.
We will use these session variables to keep user logged in, until s/he clicks log-out link.
If you are using some authentication library, login user with it instead.
*/
$_SESSION['logged_in']=$loggedin;
$_SESSION['user_name']=$user_name;
}
?>