1
votes

I have written an application that searches tweets for a specific keyword using the twitter API, but i am trying to find a way to display the latest tweets first, i am unable to find a way to sort the tweets received as a response.

I am referring to link https://dev.twitter.com/docs/api/1.1/get/search/tweets and below is my code

I have included all necessary files and set all required parameters

    function search(array $query)
{
  $toa = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
  return $toa->get('search/tweets', $query);
}

$query = array(
  "q" => "Sachin Tendulkar",
  "count" => 10,
  "result_type" => "popular"
 );

$results = search($query);

Any help on this would be appreciated. Thanks

1
I don’t see any possibility to ask for the results to be ordered in the docs … but for just 10 results, ordering them afterwards in your script should be no problem either. - CBroe
Hello Cbore, Thanks for your reply, actually 10 is just a place holder, it could be more - opensource-developer
Still doesn’t matter that much, sorting arrays is quite quick in PHP. - CBroe
OK, Yes thats true, but is there possibility that i might miss some data, i.e. say if i load 100 tweets, will there be a possibility that i might miss on some latest stuff? - opensource-developer
OK, so this is more about the selection of results in the first place, and less about ordering of the results already selected. For that they seem to offer only the result_type parameter. And if you are worried about “missing some data”, you should maybe read the comment regarding that on top of the page first … - CBroe

1 Answers

1
votes

To display the latest tweet, you should use result_type as recent.

$query = array(
  "q" => "Sachin Tendulkar",
  "count" => 10,
  "result_type" => "recent"
 );

More about result_type paramater :

  • mixed: Include both popular and real time results in the response.
  • recent: return only the most recent results in the response.
  • popular: return only the most popular results in the response.