我難以理解如何讓這個工作。我正在使用twitteroauth.php庫,並從數據庫查詢中獲取用戶令牌和令牌密鑰。下面是代碼:循環中的Redeclare函數
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);
}
}
您需要修復'TwitterOAuth',而不是重新聲明發生的地方。 – mario
不熟悉lib,但可能會聲明單個連接對象,並在每次迭代時更改它的屬性? – GabeIsman
'twitterOAuth'函數是怎麼樣的? –