2013-06-23 39 views
0

我难以理解如何让这个工作。我正在使用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); 
     } 
} 
+1

您需要修复'TwitterOAuth',而不是重新声明发生的地方。 – mario

+0

不熟悉lib,但可能会声明单个连接对象,并在每次迭代时更改它的属性? – GabeIsman

+0

'twitterOAuth'函数是怎么样的? –

回答

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); 
} 

你宣布你想进入它的变量之前,宣布它...

+0

为什么downvote? – KyleK

+0

同样的东西,它移动到下面 – nick