这是我运行的script.php,它将用户引导至callback.php
<?php
session_start();
require_once 'OAuth.php';
require_once 'OAuthCurl.php';
$callback_url = "http://something/bio/callback.html";
$consumer_key = "<code>";
$consumer_secret = "<code>";
$oauth_request_token = "http://api.twitter.com/oauth/request_token";
$oauth_authorize = "http://api.twitter.com/oauth/authorize";
$oauth_access_token = "http://api.twitter.com/oauth/access_token";
$sig_method = new OAuthSignatureMethod_HMAC_SHA1();
$test_consumer = new OAuthConsumer($consumer_key, $consumer_secret, $callback_url);
$req_req = OAuthRequest::from_consumer_and_token($test_consumer, NULL, "GET", $oauth_request_token);
$req_req->sign_request($sig_method, $test_consumer, NULL);
$oc = new OAuthCurl();
$reqData = $oc->fetchData($req_req->to_url());
parse_str($reqData['content'], $reqOAuthData);
$req_token = new OAuthConsumer($reqOAuthData['oauth_token'], $reqOAuthData['oauth_token_secret'], 1);
$acc_req = OAuthRequest::from_consumer_and_token($test_consumer, $req_token, "GET", $oauth_authorize);
$acc_req->sign_request($sig_method, $test_consumer, $req_token);
$_SESSION['oauth_token'] = $reqOAuthData['oauth_token'];
$_SESSION['oauth_token_secret'] = $reqOAuthData['oauth_token_secret'];
Header("Location: $acc_req");
?>
这是callback.php 我正在尝试在callback.php上设置一个cookie,但cookie没有设置好。 我似乎无法理解为什么?
<?php
session_start();
$_SESSION['oauth_token'];
$_SESSION['oauth_token_secret'];
require_once 'OAuth.php';
require_once 'OAuthCurl.php';
require_once __DIR__ . '/TwitterOAuth/TwitterOAuth.php';
require_once __DIR__ . '/TwitterOAuth/Exception/TwitterException.php';
use TwitterOAuth\TwitterOAuth;
date_default_timezone_set('Asia/Calcutta');
$callback_url = "http://something/bio/callback.php";
$consumer_key = "<code>";
$consumer_secret = "<code>";
$oauth_request_token = "http://api.twitter.com/oauth/request_token";
$oauth_authorize = "http://api.twitter.com/oauth/authorize";
$oauth_access_token = "http://api.twitter.com/oauth/access_token";
//$authenticate_token = "https://api.twitter.com/1.1/account/verify_credentials.json";
//echo($authenticate_token);
//exit();
$sig_method = new OAuthSignatureMethod_HMAC_SHA1();
$test_consumer = new OAuthConsumer($consumer_key, $consumer_secret, $callback_url);
$params = array();
$acc_token = new OAuthConsumer($_SESSION['oauth_token'],$_SESSION['oauth_token_secret'], 1);
$acc_req = OAuthRequest::from_consumer_and_token($test_consumer, $acc_token, "GET", $oauth_access_token);
$acc_req->sign_request($sig_method, $test_consumer, $acc_token);
$oc = new OAuthCurl();
$reqData = $oc->fetchData("{$acc_req}&oauth_verifier=".$_GET['oauth_verifier']."");
parse_str($reqData['content'], $accOAuthData);
//$username = $accOAuthData[screen_name];
$_SESSION['username'] = $accOAuthData['screen_name'];
$_SESSION['final_oauth_token'] = $accOAuthData['oauth_token'];
$_SESSION['final_oauth_token_secret'] = $accOAuthData['oauth_token_secret'];
$final_oauth_token = $_SESSION['final_oauth_token'];
$final_oauth_token_secret = $_SESSION['final_oauth_token_secret'];
$username = $_SESSION['username'];
/**
* Array with the OAuth tokens provided by Twitter when you create application
*
* output_format - Optional - Values: text|json|array|object - Default: object
*/
$config = array(
'consumer_key' => '<code>',
'consumer_secret' => '<code>',
'oauth_token' => $final_oauth_token,
'oauth_token_secret' => $final_oauth_token_secret,
'output_format' => 'object'
);
/**
* Instantiate TwitterOAuth class with set tokens
*/
$tw = new TwitterOAuth($config);
$params = array(
'screen_name' => $username,
'count' => 5,
'exclude_replies' => true
);
/**
* Send a GET call with set parameters
*/
$response = $tw->get('users/show', $params);
$id =($response-> id);
$description=($response-> description);
$screen_name = ($response-> screen_name);
setcookie("test1", $screen_name, time()+3600*30);
/*Connection String*/
$hostname = 'something';
$database = 'twitter';
$username = '';
$password = '';
$conn = mysql_connect($hostname,$username,$password);
if(!$conn)
{
die("Unable to Connect localhost!".mysql_error());
}
mysql_select_db($database) or die("Unable to select database!".mysql_error());
$query = mysql_query("SELECT id FROM twitter_tbl WHERE id = '". $id ."'");
if (mysql_num_rows($query) > 0)
{
}
else
{
$sql='INSERT INTO twitter_tbl(description,screen_name,id,final_oauth_token,final_oauth_token_secret,timestamp) VALUES("'.$description.'","'.$screen_name.'","'.$id.'","'.$final_oauth_token.'","'.$final_oauth_token_secret.'",'.strval(time()).')';
if(!mysql_query($sql,$conn))
{
die('Error: ' . mysql_error());
}
$bio_archives1 = 'INSERT INTO bio_archives(bio,screen_name,id,timestamp) VALUES("'.$description.'","'.$screen_name.'","'.$id.'",'.strval(time()).')';
if(!mysql_query($bio_archives1,$conn))
{
die('Error: ' . mysql_error());
}
}
if( strlen($description) < 80)
{
$newtext="is now ".'"'.$description.'"'." 'http://www.biostories.net/user.php?screen_name=".$screen_name."'";
$b = explode( "#tweeted", $newtext );
$txt = trim( $b[0] );
$url = trim( $b[1] );
$txt = substr( $txt, 0, 139 - strlen( $url ) );
$output = $txt . " " . $url;
}
else
{
$description1 = substr($description, 0, 50);
$newtext="is now ".'"'.$description1.'...."'." 'http://www.biostories.net/user.php?screen_name=".$screen_name."'";
$b = explode( "#tweeted", $newtext );
$txt = trim( $b[0] );
$url = trim( $b[1] );
$txt = substr( $txt, 0, 139 - strlen( $url ) );
$output = $txt . " " . $url;
}
$tw = new TwitterOAuth($config);
try{
$tw->post('statuses/update',array('status' =>$output));
}
catch(Exception $e) {
($e->getMessage());
}
$result = mysql_query('SELECT * FROM twitter_tbl WHERE id = "'.$id.'"');
while($row = mysql_fetch_array($result))
{
$desc = $row['description'];
}
if($desc === $description)
{
}
else
{
$query_update = mysql_query("SELECT id FROM twitter_up_tbl WHERE id = '". $id ."'");
if (mysql_num_rows($query_update) > 0)
{
$query1 ="Update twitter_up_tbl SET description='".$description."' WHERE id=".$id."";
$sql1 = mysql_query($query1);
}
else
{
$update_sql = 'INSERT INTO twitter_up_tbl(description,screen_name,id,final_oauth_token,final_oauth_token_secret,lastupdate) VALUES("'.$description.'","'.$screen_name.'","'.$id.'","'.$final_oauth_token.'","'.$final_oauth_token_secret.'",'.strval(time()).')';
if(!mysql_query($update_sql,$conn))
{
die('Error: ' . mysql_error());
}
}
$query ="Update twitter_tbl SET description='".$description."' WHERE id=".$id."";
$sql = mysql_query($query);
$bio_archives = 'INSERT INTO bio_archives(bio,screen_name,id,timestamp) VALUES("'.$description.'","'.$screen_name.'","'.$id.'",'.strval(time()).')';
if(!mysql_query($bio_archives,$conn))
{
die('Error: ' . mysql_error());
}
}
?>