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

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.

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

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,