Im working with the Twitter API and havent found out how to get the tweets from only the last hour for example?
I cannot figure it out how to get tweets only from last 24 or from last 1 hour.. Can someone help me?
Here is my script. This script counts the tweets currently but from last 7 days as normal...
<?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']
). When the value is more than 24 hours old exit the outer while
loop.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.