【问题标题】:Twitter API - Get tweets from last 24 hours or last hourTwitter API - 获取过去 24 小时或过去一小时的推文
【发布时间】:2017-09-07 03:41:44
【问题描述】:

我正在使用 Twitter API,但还没有发现如何从最后一小时获取推文?

我无法弄清楚如何仅从过去 24 或过去 1 小时获取推文。有人可以帮助我吗?

这是我的脚本。此脚本正常计算当前但过去 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;      
?>

【问题讨论】:

    标签: php api twitter web-deployment


    【解决方案1】:

    在这个循环中

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

    检查推文的日期/时间 ($tweet['created_at'])。当值超过 24 小时时退出外部 while 循环。

    【讨论】:

    • 谢谢。如何正确地做到这一点? Twitter“created_at”日期格式似乎很奇怪.. :/
    • 现在是 UTC 时间(格林威治标准时间)。因此,要么将其转换为您的本地时间,要么将您的本地时间转换为 UTC。有什么奇怪的?
    • 嗯,将它转换为我的时间的最佳方法是什么?非常感谢。
    • 这可能会有所帮助 - stackoverflow.com/questions/32904530/…
    猜你喜欢
    • 1970-01-01
    • 2012-07-05
    • 2014-09-17
    • 2018-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-23
    相关资源
    最近更新 更多