dsoy71058 2017-09-05 07:52
浏览 226
已采纳

Twitter API - 如何在过去24小时内按标签计算推文?

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>';

}            
?>
  • 写回答

1条回答 默认 最新

  • dsz90288 2017-09-05 08:46
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?