5
votes

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. 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.

  2. 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?

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

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++;
    }
}
1

1 Answers

2
votes

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!

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;