close
The Wayback Machine - https://web.archive.org/web/20130508091326/https://dev.twitter.com/issues/996

Issue #996: Twitter 1.1 search API ignores max_id field - Appears to be an issue with max_id being at the front of the CGI params before the OAuth tokens

The twitter search API for "/search/tweets" is ignoring the max_id field. I think its because the OAuth.php library sorts the CGI params according to RFC 9.1.1, causing the max_id field to be listed before the OAuth tokens and the q parameter, and so you dont process the max_id field. If I edit the code to put the max_id at the end of the CGI params, it sometimes works and sometimes gives me an authentication error, so I cannot reliably use that as a work around either.

Here is the PHP code:

  1.     function search($query, $max_id)
  2.     {     
  3.         $toa = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
  4.  
  5.         // echo "Searching with MAX ID " . $max_id . "\n";
  6.         if ($max_id != null) {
  7.             return $toa->get('/search/tweets', array('q' => $query, 'count' => 100, 'max_id' => $max_id));
  8.         } else {
  9.             return $toa->get('/search/tweets', array('count' => 100, 'q' => $query));
  10.         }
  11.     }
  12.  
  13.     do {
  14.         $results = search($argv[1], $max_id);
  15.  
  16.         if (isset($results->errors)) {
  17.             if (!strcmp($results->errors[0]->message, "Rate limit exceeded")) {
  18.                 echo "Pausing...\n";
  19.                 sleep(300);
  20.                 $results = search($argv[1], $max_id);
  21.                 continue;
  22.             } else {
  23.                 var_dump($results);
  24.                 exit;
  25.             }
  26.         }
  27.  
  28.         if ($verbose) {
  29.             var_dump($results);
  30.         }
  31.  
  32.         $count = 0;
  33.         foreach ($results->statuses as $result) {
  34.             echo str_replace("", " ", $result->id_str) . "";
  35.             echo str_replace("", " ", $result->created_at) . "";
  36.             echo str_replace("", " ", $result->user->location) . "";
  37.             echo str_replace("", " ", $result->user->followers_count) . "";
  38.             echo str_replace("*", " ", $result->text) . "\n";
  39.             $count++;
  40.         }
  41.  
  42.         if ($count == 0) {
  43.             echo "done\n";
  44.             exit;
  45.         }
  46.  
  47.         sleep(10);
  48.  
  49.         $max_id = $results->search_metadata->max_id_str;
  50.  
  51.         $pages++;
  52.     } while ($pages < $MAX);