简体   繁体   English

Twitter API-获取过去24小时或过去1小时的推文

[英]Twitter API - Get tweets from last 24 hours or last hour

Im working with the Twitter API and havent found out how to get the tweets from only the last hour for example? 我正在使用Twitter API,还没有找到例如仅从最后一个小时获取推文的方法?

I cannot figure it out how to get tweets only from last 24 or from last 1 hour.. Can someone help me? 我无法弄清楚如何仅从最近24小时或从最近1小时获得推文。有人可以帮助我吗?

Here is my script. 这是我的剧本。 This script counts the tweets currently but from last 7 days as normal... 该脚本对当前但过去7天以来的推文进行正常计数...

<?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' => "XXXXX",
    'oauth_access_token_secret' => "YYYYY",
    'consumer_key' => "XXXX",
    'consumer_secret' => "YYYY"
);


$query = '%24synx';
$max_id = 0;
$tweets_count = 0;

while (true) {
  // First API call
  if ($max_id == 0) {
    $url = 'https://api.twitter.com/1.1/search/tweets.json';
    $getfield = "?q=".$query."&count=100";
    $requestMethod = 'GET';
    $twitter = new TwitterAPIExchange($settings);
    $twitter_data = json_decode($twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest(), true);

   // Repeated API call
   } else {
    // Collect older tweets
    --$max_id;

    $url = 'https://api.twitter.com/1.1/search/tweets.json';
    $getfield = "?q=".$query."&count=100&max_id=".$max_id."";
    $requestMethod = 'GET';
    $twitter = new TwitterAPIExchange($settings);
    $twitter_data = json_decode($twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest(), true);
  }          

$tweets = $twitter_data['statuses'];

print_r($tweets);

// Exit loop when no more tweets are returned
if (sizeof($tweets)==0) {
    break;
  }

    // Count tweets
    foreach($tweets as $tweet) {
    ++$tweets_count;
    $max_id = $tweet['id'];
  } 

//Sleep 3 seconds after every API call, to not exceed API rate limit
sleep(3);  
}


echo $tweets_count;      
?>

In this loop 在这个循环中

foreach ($tweets as $tweet) {
    ++$tweets_count;
    $max_id = $tweet['id'];
}         

Check the date/time of the tweet ( $tweet['created_at'] ). 检查tweet的日期/时间( $tweet['created_at'] )。 When the value is more than 24 hours old exit the outer while loop. 如果该值超过24小时,则退出外部while循环。

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

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