2012-11-11 59 views
0

我正在使用以下代碼在mysql中存儲我的Facebook應用用戶詳細信息,以便他們能夠在我的網站上查看獨家內容。將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; 
} 

?> 
+0

如果你想在離線時在用戶的牆上發佈你只需要'publish_stream'權限,你**不需要**甚至需要用戶訪問令牌......直接使用PHP-SDK並且它將自動添加應用程序訪問令牌,這將工作。 P.S .:你的代碼需要注意。 – ifaour

回答

0

我不明白。您已經保存了令牌。

你需要的是,如果用戶已經登錄,在驗證過的頁面中驗證,如果是的話,你將爲他的$ _SESSION ['user_id'](例如)返回令牌,並創建在Facebook API中使用該令牌進行的交互,例如使用:

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

我希望它對您有幫助。

+0

我正在尋找離線令牌。即使他沒有連接到FB,我也想在用戶的牆上張貼消息。 –

+0

您可以這樣做,您還需要詢問publish_stream權限以使其在訪問令牌中被允許。我建議你在保存之前詢問擴展訪問令牌(在60天后過期),使用: $ facebook-> setExtendedAccessToken(); $ fb_access_token = $ facebook-> getAccessToken(); 不要忘記令牌過期!如果令牌無效並且用戶需要刷新它,您總是需要有一個異常。 –

+0

在config.php文件中我已要求'publish_stream,offline_access'權限。我的應用可以在用戶的​​牆上發佈消息,但我希望能夠隨時向所有應用用戶發送消息。就像在這個應用程序,如果我想更新用戶牆http://yougapi.com/products/facebook_viral/admin/ –

相關問題