我有3页,button.php,login-twitter.php和getTwitterData.php。
我想要获得的流程是从我的网站登录用户到Twitter,并在他们的推特墙上发布推文(网站应该自动执行,用户无需键入)然后重定向他们回到网站
在button.php上,我有用于登录用户的按钮的代码
<?
echo "<div class='col-md-9'>";
echo "<a href='login-twitter.php'><button>Post link on Twitter</button></a>";
echo "</div>";
?>
用户被重定向到login-twitter.php页面
<?php
require("twitter/twitteroauth.php");
require 'twconfig.php';
session_start();
$twitteroauth = new TwitterOAuth(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET);
$request_token = $twitteroauth->getRequestToken('http://abc/svy/getTwitterData.php');
$_SESSION['oauth_token'] = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
if ($twitteroauth->http_code == 200) {
$url = $twitteroauth->getAuthorizeURL($request_token['oauth_token']);
header('Location: ' . $url);
} else {
die('Something wrong happened.');
}
?>
getTwitterData.php的代码
<?php
ob_start();
require("twitter/twitteroauth.php");
require 'twconfig.php';
session_start();
if (!empty($_GET['oauth_verifier']) && !empty($_SESSION['oauth_token']) && !empty($_SESSION['oauth_token_secret']))
{
$twitteroauth = new TwitterOAuth(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
$access_token = $twitteroauth->getAccessToken($_GET['oauth_verifier']);
$_SESSION['access_token'] = $access_token;
$user_info = $twitteroauth->get('account/verify_credentials');
//header("Location:http://www.twitter.com");
}
else
{
//header('Location: u_tasks.php');
}
?>
在这个页面中,我想要做的是在用户的墙上发布数据,所以在行之后
$user_info = $twitteroauth->get('account/verify_credentials');
在getTwitterData.php页面上我尝试输入以下代码
$tweet = new TwitterOAuth(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
// Set status message
$tweetMessage = 'message to be posted';
// Check for 140 characters
if(strlen($tweetMessage) <= 140)
{
// Post the status message
$tweet->post('statuses/update', array('status' => $tweetMessage));
}
我的代码只是登录用户,但没有在他们的墙上发布推文。有人可以帮我解决代码吗