简体   繁体   English

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

[英]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. 我尝试使用Twitter API,我想计算过去24小时内有特殊标签在Twitter上发布的推文数量。

I couldnt really find a way to do so? 我真的找不到办法吗? It seems to be limited to 100? 好像限制为100个?

I need to get the amount of tweets in the last 24 hours in order to setup some stats on my website. 为了在我的网站上设置一些统计信息,我需要获取过去24小时内的推文数量。

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. Twitter REST API已经表明了这一点。 (I know you're using the Stream API, but let me explain). (我知道您正在使用Stream API,但让我解释一下)。

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. 由于Twitter API均遵循API开发人员自己设置的一般规则,因此我假设Stream API遵循与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. GET搜索/推文的文档指出(参数: count ),返回的推文数量限制为100。即使流和REST API之间没有明显的联系,即使文档中未对此进行描述,但我d表示,这就是数组中只有100个元素的原因。

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. 如果可能,您可以运行一个脚本,该脚本以一定的间隔将请求发送到服务器,然后将各个tweet保存在数据库/文本文件/任何文件中。 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分钟15次或每15分钟180次,因此这是一种解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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