我很难过如何让这个工作。我正在使用twitteroauth.php库,我从数据库查询中获取用户令牌和令牌密钥。这是代码:
while($row = mysql_fetch_array($m))
{
$connection = new TwitterOAuth($consumerKey, $consumerSecret, $oauthToken, $oauthTokenSecret);
$consumerKey = "#####";
$consumerSecret = "#####";
$oauthToken = $row['oauth_token'];
$oauthTokenSecret = $row['oauth_token_secret'];
$userName = $row['username'];
$query = mysql_query("select url from retweets order by rand() limit 1");
$row = mysql_fetch_assoc($query);
$retweet = basename($row['url']);
$method = 'statuses/retweet/'.$retweet.'';
twitterOAuth($method, $connection->post($method), $connection->http_code, $userName);
}
如果mysql有多个结果,它会循环显示并说:
Fatal error: Cannot redeclare random_from() (previously declared in /usr/share/nginx/www/twitteroauth/twitteroauth.php:199) in /usr/share/nginx/www/twitteroauth/twitteroauth.php on line 199
由于$connection
变量依赖于mysql查询中的$oauthToken
和$oauthTokenSecret
,我无法将其移到循环之外,所以我如何才能使它变为可以使用它在每个循环中没有重新声明?
提前谢谢!
更新:这是twitterOAuth
function twitterOAuth($method, $response, $http_code, $userName, $parameters = '') {
if($http_code == 200){
echo "retweeted successfully @".$userName."<br />";
}else{
echo "an error has occurred while retweeting @".$userName."";
echo "<br />";
print_r($response);
}
}
答案 0 :(得分:0)
它需要看起来像这样......
while($row = mysql_fetch_array($m))
{
$consumerKey = "#####";
$consumerSecret = "#####";
$oauthToken = $row['oauth_token'];
$oauthTokenSecret = $row['oauth_token_secret'];
$userName = $row['username'];
$connection = new TwitterOAuth($consumerKey, $consumerSecret, $oauthToken, $oauthTokenSecret);
$query = mysql_query("select url from retweets order by rand() limit 1");
$row = mysql_fetch_assoc($query);
$retweet = basename($row['url']);
$method = 'statuses/retweet/'.$retweet.'';
twitterOAuth($method, $connection->post($method), $connection->http_code, $userName);
}
您在声明要传递给它的变量之前声明了它......