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),
)";