【问题标题】:Total tweet counter based on hashtags基于主题标签的总推文计数器
【发布时间】:2014-12-11 07:24:19
【问题描述】:

我正在基于 php 中的当前推文计数构建一个包含特定主题标签的事件。 在主页上,我有一个计数器,上面写着:“到目前为止的推文:xxx”。我使用了这个 php 脚本:

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);

接下来,我将结果保存在我的数据库中:

$updateCount = "UPDATE tweets SET currentcount = '$total'";   
$doQuery = mysql_query($updateCount, $con) or die(mysql_error());

此脚本由每 2 分钟触发一次脚本的 cronjob 运行。 它工作正常。但是这个脚本只返回 1500 条推文,因为 twitter 不再允许。

我如何仍能跟踪当前的推文数量?我知道没有办法超过 twitter api 的最大推文。但也许通过时间或数据库检查? 提前致谢!

【问题讨论】:

    标签: php mysql twitter


    【解决方案1】:

    我会怎么做:

    • 用当前计数保存您在数据库中获得的最后一条推文 ID。
    • 在下一次运行时,从该推文中获取推文,将推文数量添加到计数中,并使用新的最后一条推文更新最后一条推文条目。
    • 起泡、冲洗、重复。

    该方法的一个问题:如果在不到 2 分钟的时间内有超过 1500 条推文,有些将不会被计算在内。

    要从上一条推文中获取推文,您可以使用since_id 参数:

    since_id:返回状态 id 大于给定 id 的推文。

    Twitter Search API

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-31
      • 2012-05-18
      • 1970-01-01
      • 2019-01-14
      • 2015-07-08
      • 1970-01-01
      • 2012-12-03
      • 1970-01-01
      相关资源
      最近更新 更多