使用PHP计算带有给定标签的推文


Counting tweets with a given hashtag using PHP

我正在尝试使用PHP计算带有给定标签的推文数量。这段源代码是由StackOverflow上的某个人提供的。我需要添加任何库或更改任何设置才能使函数工作吗?因为当我运行这个代码时,它只会给我一个空白页。

<?php
   global $total, $hashtag;
   //$hashtag = '#supportvisitbogor2011';
   $hashtag = '#australialovesjustin';
   $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);
      //echo "<pre>";    
      //$json_decode = json_decode($json);
      //print_r($json_decode->results);
      $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);
   echo $total;
?>

阅读这些文章

  • https://dev.twitter.com/docs/using-search
  • https://dev.twitter.com/docs/api/1/get/search

尝试

developer console

相关文章: