I’m building an event based on the current tweet count in php that holds a particular hashtag.
On the main page I have a counter that says: “tweets so far: xxx”. I used this php script:
global $total, $hashtag;
$hashtag = '#somehashtag';
$total = 0;
function getTweets($hash_tag, $page) {
global $total, $hashtag;
$url = 'http://search.twitter.com/search.json?q='.urlencode($hash_tag).'&';
$url .= 'page='.$page;
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
$json = curl_exec ($ch);
curl_close ($ch);
$json_decode = json_decode($json);
$total += count($json_decode->results);
if($json_decode->next_page){
$temp = explode("&",$json_decode->next_page);
$p = explode("=",$temp[0]);
getTweets($hashtag,$p[1]);
}
}
getTweets($hashtag,1);
Next, I save the result in my database:
$updateCount = "UPDATE tweets SET currentcount = '$total'";
$doQuery = mysql_query($updateCount, $con) or die(mysql_error());
This script runs by a cronjob that fires the script every 2 minutes.
It works fine. But this script returns only 1500 tweets since twitter doesn’t allow any more.
How can I still keep track of the current tweet count? I know there is no way of going above the max tweets of the twitter api. But maybe through time or database checks?
Thanks in advance!
The way I would do it:
One problem with the approach: if there are more than 1500 tweets in less than 2 minutes, some will not be counted.
To get the tweets from your last tweet on, you can use the
since_idparameter:Twitter Search API