Using max_id and since_id correctly in search api



I’m fairly new to the Twitter API and I’m trying to use the search/tweets endpoint to get some tweets.

So, this is my problem and I was wondering If someone could possibly point me in the right direction:

If I poll twitter at 17:00 and there are 305 tweets, then add all of these tweets to the database storing the tweet id and a bunch of other info about the tweet.

Then If i poll twitter again at 17:30 and there are now 350 tweets, then log the tweets starting from 306 onwards.

I have something like the following:

$max_id = 0;

while(true) {
  // first API call
  if ($max_id = 0) {
    // do twitter search
    $results = ’TWITTER_SEARCH_HERE’;
  } else {
    // Get results but pass in the max_id

  $tweets = $results->statuses;

  $totalResults = array_merge($results, $tweets);

  $max_id = $statuses->search_metadata->max_id;

I’m not sure if I’m doing this right though, or passing in the correct max_id values.

Is anyone able to help me?



For the second request you should set since_id correctly, that should make sure that you don’t get Tweets you already are aware of. But note it’s possible that you get not all tweets in any case, as the search API is very limited and only contains a “small” amount of all tweets.