5

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 Answers1

2

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;
Kerem
  • 11,377
  • 5
  • 59
  • 58
  • Thanks, it looks promising. I just tested it quickly though, and it shows an error for the last row: `Trying to get property of non-object... Undefined offset: -1`. I don't understand what that line does, so I don't know how to interpret the error message. –  Jan 20 '13 at 23:37
  • 1
    I placed the code you added to your answer after the rest. Both alternatives just show empty arrays :( Also, I got a new error message for the same line (after trying to remove the @-sign): "Fatal error: Cannot use object of type stdClass as array" –  Jan 21 '13 at 00:20
  • Try to convert `$content` to array, doing `$content = (array) $content;` – Kerem Jan 21 '13 at 00:36
  • I'm not getting it to work. At what point should I try to convert `$content` into an array? –  Jan 21 '13 at 00:51
  • I finally understood what you were doing, and came up with something similar, though a lot uglier and less well behaving. I posted the code above. –  Jan 21 '13 at 02:00
  • Good to see this, cos my pseudo was only a clue for an idea on how to. Congrats! – Kerem Jan 21 '13 at 15:34