如何获得3,200条推文(Twitter API 1.1) [英] How to get the 3,200 tweets (Twitter API 1.1)

查看:219
本文介绍了如何获得3,200条推文(Twitter API 1.1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过大量的试验和错误,我终于设法使用Twitters新API(版本1.1)提取tweet。我使用PHP TwitterOauth库。即使我可以抓取tweet,我还是不明白两件事。

After a lot of trial and error, I have finally managed to fetch tweets with Twitters new API (version 1.1). I'm using the PHP TwitterOauth library. Even though I'm able to fetch tweets, two things I do not understand.


  1. 状态/ user_timeline的限制是200微博。如何循环通过结果获取最大数量的3,200条tweets?我读了一些关于做多个GET请求,但是我不知道该怎么做。

  1. The limit for statuses/user_timeline is 200 tweets. How do I loop through the results to fetch the maximum number of 3,200 tweets? I read something about making multiple GET requests, but I'm not sure how to do that.

看起来tweets的数量是随机变化的,很少实际上达到参数count中指定的数字。为什么呢?

It seems the number of tweets fetched varies randomly, and seldomly actually gets up to the number specified in the parameter 'count'. Why is that?



我的应用程序只是让访问者输入用户名并获取该用户的tweets。这是我的代码。

My application simply lets the visitor type in a username and fetch the tweets of that user. Here's my code.

if (isset($_GET['user'])) {
   $user = $_GET['user'];
   $content = $connection->get("statuses/user_timeline", array('count' => 200, 'exclude_replies' => true, 'screen_name' => $user));?>

   $j = 0;
   foreach ($content as $tweet) {
      echo $j.' '.$tweet->text. '<br />';
      $j++;
   } 
}



更新:在尝试下面的queremys建议后,一个真正丑陋的解决方案,有几个主要的缺点。至少它显示最大量3,200条tweets(和一些重复)。如果有问题的Twitter帐户有少于3,200条推文,结果会显得很奇怪。

UPDATE: After trying out queremys suggestion below, I came up with a really ugly looking "solution" that has several major drawbacks. At least it shows the maximum amount of 3,200 tweets (and some duplicates). The result will look weird if the twitter account in question has less than 3,200 tweets. Anyways, just thought I'd share it if it can be of inspiration.

if (isset($_GET['user'])) {
    $user = $_GET['user'];

    $content = $connection->get('statuses/user_timeline', array(
    'count' => 200, 'exclude_replies' => true, 'screen_name' => $user, 'include_rts' => 1
));

    $x = 0;
    while ($x < 15) {
        $text = array();

        foreach ($content as $tweet) {
            $text[] = $tweet->id_str;
            echo $tweet->text.'<br />';
        }

        $last_tweet = end($text);

        $content = $connection->get('statuses/user_timeline', array(
    'count' => 200, 'exclude_replies' => true, 'screen_name' => $user, 'include_rts' => 1, 'max_id' => $last_tweet
));
        foreach ($content as $tweet) {
            echo $tweet->text.'<br />';
        }
        $x++;
    }
}


推荐答案

可能有点贵,但我认为可能(你可以测试这样的东西);

It could be a little bit pricy but i think possible (you can test something like that);

$contents = array();
$limit = 3200;
$max_id = null;
for ($count = 200; $count < $limit; $count += 200) {
    if (null !== $max_id && $max_id == '') {
        break;
    }

    $content = $connection->get('statuses/user_timeline', array(
        'count' => $count, 'exclude_replies' => true, 'screen_name' => $user,
        'max_id' => $max_id
    ));
    $contents[] = $content;
    // this indicates the last index of $content array
    $max_id = $content[count($content) - 1]->id_str;
}

UPDATE!

您需要使 $ max_id 才能继续循环,并需要 $ max_id NULL来中断循环。

You need to make $max_id to continue loop, and need to $max_id NULL to break loop.

// option 1, makes $max_id NULL silently
@ $max_id = $content[count($content) - 1]->id_str;

// option 2, search for last index of array
if (count($content)) {
    $last_tweet = end($content);
    $max_id = $last_tweet->id_str;
} else $max_id = null;

这篇关于如何获得3,200条推文(Twitter API 1.1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆