我目前正在使用TwitterOAuth中的会话(这仍然是一个会话问题)。我有两个简单的文件:
twitter.php:
<?php
session_start();
require 'autoload.php';
use Abraham\TwitterOAuth\TwitterOAuth;
$handle = @fopen("...file path here...", "r");
$credentials = array("", "", "", "");
$counter = 0;
if($handle) {
while(($buffer = fgets($handle, 4096)) !== false) {
$split = explode(" ", $buffer);
$credentials[$counter++] = preg_replace('/\s+/', '', $split[1]);
}
if(!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
define('CONSUMER_KEY', $credentials[0]); // add your app consumer key between single quotes
define('CONSUMER_SECRET', $credentials[1]); // add your app consumer secret key between single quotes
define('OAUTH_CALLBACK', 'http://outsidetheblock.org/twitterbot/twitter/callback.php'); // your app callback URL
// ********* ABOVE WAS JUST LOADING KEYS *********
// ********* IMPORTANT STUFF BELOW HERE *********
// ********* IMPORTANT STUFF BELOW HERE *********
// ********* IMPORTANT STUFF BELOW HERE *********
// ********* IMPORTANT STUFF BELOW HERE *********
if(!isset($_SESSION['access_token'])) {
// ALWAYS RAN, if statement is always true
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
$request_token = $connection->oauth('oauth/request_token', array('oauth_callback' => OAUTH_CALLBACK));
$_SESSION['oauth_token'] = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
$url = $connection->url('oauth/authorize', array('oauth_token' => $request_token['oauth_token']));
header('Location: ' . $url);
} else {
$access_token = $_SESSION['access_token'];
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
$user = $connection->get("account/verify_credentials");
echo $user->screen_name;
}
callback.php:
<?php
session_start();
require 'autoload.php';
use Abraham\TwitterOAuth\TwitterOAuth;
if(isset($_REQUEST['oauth_verifier'], $_REQUEST['oauth_token'])) {
echo "1. Yes";
}
echo "</br>2. " . $_REQUEST['oauth_token'];
echo "</br>3. " . $_SESSION['oauth_token'];
if(isset($_REQUEST['oauth_verifier'], $_REQUEST['oauth_token']) && $_REQUEST['oauth_token'] == $_SESSION['oauth_token']) {
echo "2"; // NEVER RAN, if statement always false
$request_token = array();
$request_token['oauth_token'] = $_SESSION['oauth_token'];
$request_token['oauth_token_secret'] = $_SESSION['oauth_token_secret'];
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $request_token['oauth_token'], $request_token['oauth_token_secret']);
$access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier']));
$_SESSION['access_token'] = $access_token;
header('Location: ../');
}
callback.php的输出如下:
1. Yes
2. bTT3owAAAAAAvpYyAAABVbRJLcQ
3.
据我所知,问题是callback.php中if语句的下半部分。似乎&#34; $ _ SESSION [&#39; oauth_token&#39;]&#34;总是空白的。有想法该怎么解决这个吗?我哪里错了?