0

次のコードを使用して、Facebookアプリのユーザーの詳細をmysqlに保存し、Webサイトの独占的なコンテンツを表示できるようにします。

私の質問は簡単です。ユーザーのアクセストークンを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 に答える