简体   繁体   中英

Twitter API - How to count Tweets by hashtag in last 24 hours?

I tried using the Twitter API and I want to count how many tweets are being tweeted in the last 24 hours containing a special hashtag.

I couldnt really find a way to do so? It seems to be limited to 100?

I need to get the amount of tweets in the last 24 hours in order to setup some stats on my website.

Current code:

<?php
ini_set('display_errors', 1);
require_once('TwitterAPIClass/TwitterAPIExchange.php');

/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
    'oauth_access_token' => "XXXX",
    'oauth_access_token_secret' => "YYYY",
    'consumer_key' => "XXXX",
    'consumer_secret' => "YYYY"
);    

/** Perform a GET request and echo the response **/
/** Note: Set the GET field BEFORE calling buildOauth(); **/
$url = 'https://stream.twitter.com/1.1/statuses/filter.json';
$getfield = '?track=%23beer';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$twitter_data = json_decode($twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest(), true);

foreach($twitter_data['statuses'] as $tweets) {
    echo $tweets['created_at'] . '<br>';

}            
?>

This may not be the perfect answer, but I'm assuming that your assumption is correct. The Twitter REST APIs are an indicator for that already. (I know you're using the Stream API, but let me explain).

Since the Twitter APIs all follow general rules that are set up by the APIs developers themselves, I'm assuming that the Stream API follows the same regulations as the REST API. The documentation of GET search/tweets states (parameter: count ) that the amount of tweets returned is limited to 100. And even though there's no obvious connection between the streaming and the REST API and even though it's not described in the documentation, I'd say that that's the reason why there are only 100 elements in your array.

If it's possible for you, you could run a script that sends a request to the server in a certain interval, and then saves the individual tweets in a database/text file/whatever. Since the regulation of request frequency are set to 15 times per 15 minutes or 180 times per 15 minutes, this would be a workaround.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM