2013-07-01 26 views
0

我似乎无法将Twitter user_id,screen_name,oauth_token和oauth_token_secret(从登录用户)保存到我设置的mysql数据库中?将Twitter标记存储在mysql数据库中

我正在使用亚伯拉罕威廉斯奥阿斯图书馆。

这段代码工作正常,我可以在用户登录时通过使用print_r请求来查看组成access_token的组件,但是令牌不会保存到数据库'令牌'的表'users'中?

我已经阅读了几乎所有关于SO的问题和答案,并且测试了代码的每一点,但是我似乎无法得到一个简单的INSERT为这些标记工作?我也将一些测试组件硬编码到config_db文件(作为INSERT),并且它们加载正常。

回调代码:

<?php 

require_once("/path/config_db.php"); 

session_start(); 
// Include class & create 
require_once('/path/config.php'); 
require_once('/path/twitteroauth/twitteroauth.php'); 
// User has selected to DENY access 
if(!empty($_GET["denied"])) { 
    // could re-direct or display cancelled view/template 
    // we're just echoing out a message 
    echo "No deal! <a href='index.php'>Try again?</a>"; 
    die(); 
} 

/* If the oauth_token is old redirect to the connect page. */ 
if (isset($_REQUEST['oauth_token']) && $_SESSION['oauth_token'] !== $_REQUEST['oauth_token']) { 
    $_SESSION['oauth_status'] = 'oldtoken'; 
    header('Location: ./clearsessions.php'); 
} 

/* Create TwitteroAuth object with app key/secret and token key/secret from default phase */ 
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']); 

/* Request access tokens from twitter */ 
$access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']); 

//echo "RECEIVED TOKENS<br>"; 
// Check we have valid response 
if(is_numeric($access_token["user_id"])) { 
// Save the access tokens to a DB (we're using a session) 
/* Save the access tokens. Normally these would be saved in a database for future use. */ 
$_SESSION['access_token'] = $access_token; 

//GET CREDENTIALS VIA API 
$credentials = $connection->get('account/verify_credentials'); 

//insert tokens into db 
print_r($_SESSION["access_token"]); 
$sql="INSERT INTO users (`user_id` ,`screen_name` ,`oauth_token` ,`oauth_token_secret`) 
VALUES ('".$_SESSION["access_token"]["user_id"]."', 
'".$_SESSION["access_token"]["screen_name"]."', 
'".$_SESSION["access_token"]["oauth_token"]."', 
'".$_SESSION["access_token"]["oauth_token_secret"]."'"; 

if (!mysqli_query($con,$sql)) 
    { 
    die('Error: ' . mysqli_error($con)); 
    } 
echo "1 record added"; 

} 
//echo $query; 
//echo mysql_error(); 
print_r($_SESSION["access_token"]); 
$message = array('status' => 'Test OAuth update. #testoauth'); 
$test = $connection->post('statuses/update', array('status' => 'Just a test ')); 

/* Remove no longer needed request tokens */ 
unset($_SESSION['oauth_token']); 
unset($_SESSION['oauth_token_secret']); 

/* If HTTP response is 200 continue otherwise send to connect page to retry */ 
if (200 == $connection->http_code) { 
    /* The user has been verified and the access tokens can be saved for future use */ 
    $_SESSION['status'] = 'verified'; 
    header('Location: ./callback.php'); 
} else { 
    /* Save HTTP status for error dialog on connnect page.*/ 
    header('Location: ./clearsessions.php'); 
} 

?> 

<? print_r($access_token); ?> 

连接(config_db文件)如下

<?php 

$con=mysqli_connect("server","username","password","tokens"); 
// Check connection 
if (mysqli_connect_errno()) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 

mysqli_close($con); 

?> 

表 '用户' 如下:

$sql = "CREATE TABLE users 
(
user_id INT(11), 
screen_name varchar(50), 
oauth_token varchar(90), 
oauth_token_secret varchar(90), 
)"; 

回答

0

你有一个语法SQL查询错误。你忘记了小括号。

'".$_SESSION["access_token"]["oauth_token_secret"]."'"; change this 
'".$_SESSION["access_token"]["oauth_token_secret"]."')";