0

我正在使用以下代码将我的 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;
 }

 ?>
4

1 回答 1

0

我不明白。您已经在保存令牌。

您需要做的是在仅经过身份验证的页面中验证用户是否已登录,如果是,您将返回他的 $_SESSION['user_id'] 的令牌(例如),并在 Facebook 中创建交互具有该令牌的 API,例如:

$token = $_SESSION['token'];
$facebook->setAccessToken($token);

我希望它对你有所帮助。

于 2012-11-11T15:08:13.990 回答