我正在使用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;
?>
答案 0 :(得分:0)
在这个循环中
foreach ($tweets as $tweet) {
++$tweets_count;
$max_id = $tweet['id'];
}
检查推文的日期/时间($tweet['created_at']
)。当值超过24小时时,退出外部while
循环。