我使用以下代码在mysql中存储我的facebook app用户详细信息,以便他们在我的网站上查看独家内容。
我的问题很简单,我怎样才能将用户的访问令牌存储在我的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;
}
?>
答案 0 :(得分:0)
我不明白。您已经在保存令牌。
您需要的是,如果用户已登录,则在仅经过身份验证的页面中进行验证,如果是,则返回其$ _SESSION ['user_id']的令牌(例如),并创建Facebook API与该令牌之间的交互,例如:
$token = $_SESSION['token'];
$facebook->setAccessToken($token);
我希望它确实对你有帮助。