简体   繁体   English

如何使用Twitter的API(1.1)设置最大限制(3200条推文)

[英]How do I the max limit (3200 tweets) using Twitter's API (1.1)

I have been able to do it but it's in a rather 'ugly' way. 我已经能够做到,但是它的方式相当“丑陋”。

One problem I'm facing with refactoring is that I need to call 'max_id' but I need to use a placeholder until I make the first call. 我在重构时面临的一个问题是我需要调用“ max_id”,但是我需要使用占位符,直到我进行第一个调用。 I've tried using 'null' and '' for $max_id but neither work. 我已经尝试过为$ max_id使用'null'和'',但是都没有用。 Also, I have a feeling it could be written without using a manual 'for' loop. 另外,我有一种感觉,无需使用手动的“ for”循环就可以编写它。

So far this is my code: 到目前为止,这是我的代码:

  $i = 1;
  $content = $connection->get('statuses/user_timeline', array('screen_name' => 'username', 'count' => '200'));
  foreach ($content as $item) {
    echo $i . " : " . $item->text;
    echo("<hr>");
    $i++;
  }
  $lastTweet = end($content);
  $max_id = $lastTweet->id_str;

  for ($l = 0; $l<15; $l++) {
    $content = $connection->get('statuses/user_timeline', array('screen_name' => 'username', 'count' => '200', 'max_id' => $max_id));
    foreach ($content as $item) {
      echo $i . " : " . $item->text;
      echo("<hr>");
      $i++;
    }
    $lastTweet = end($content);
    $max_id = $lastTweet->id_str;
  }

Any help would be appreciated. 任何帮助,将不胜感激。

I know it's not beautiful, but inside the loop couldn't you just set a test variable. 我知道它并不漂亮,但是在循环内部您不能只设置一个测试变量。 Sort of like a first time through instance or will that ruin the point of refactoring? 就像是第一次通过实例进行操作,还是会破坏重构点?

$maxID = false;
if(!$maxID) $content = $connection->get('statuses/user_timeline', array('screen_name' => 'username', 'count' => '200'));
else { $content = $connection->get('statuses/user_timeline', array('screen_name' => 'username', 'count' => '200', 'max_id' => $max_id)); 
$maxID = true; } 

Instead of a for loop if you're careful you could probably get away with a while loop() that goes off the count of your items returned then just increment that variable by the number of tweets returned on each pass. 如果您不小心,则可以使用一个while loop()来代替返回循环,而while loop()会使返回的项目计数减少,然后只需将该变量增加每次通过时返回的推文数量即可。 Just never accidentally let your while loop run forever, or you can say goodbye to twitter api requests for awhile. 只是永远不要意外地让您的while循环永远运行,或者您可以暂时告别Twitter api请求。

$countTweets = 0;
 while($countTweets < 3200)
 { //code for your calls, do what you gotta do
   $countTweets += count($content);
 }

I use something very similar to what you have, except i just merge the next set of tweets to a total array then work off that big list of tweets. 我使用与您所拥有的非常相似的东西,除了我只是将下一组推文合并到一个总数组中,然后处理大量推文。 I also just test count of total merged array instead of adding to a count variable. 我也只测试合并数组的总数,而不是添加到count变量中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM