我在弄清楚如何返回在 Twitter 上使用主题标签的总次数时遇到问题。过去,我使用以下代码确实有效,但“ http://search.twitter.com/search.json ”地址已被 Twitter 停用。旧代码是:
<?php
global $total, $hashtag;
//$hashtag = '#supportvisitbogor2011';
$hashtag = '#MyHashtag';
$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;
?>
我知道您知道必须使用授权的 Twitter 应用程序并有权获取数据。我能够设置应用程序,并且可以使用以下代码提取数据列表,但我不确定如何使用该数据得出总计数。有人可以通过更改我拥有的代码或帮助我了解我应该如何去做来帮助我获得总数。这是我提取主题标签数据的代码:
<?php
session_start();
require_once("twitteroauth.php"); //Path to twitteroauth library
$hashtag = "MyHashtag";
$consumerkey = "MYINFOWOULDBEHERE";
$consumersecret = "MYINFOWOULDBEHERE";
$accesstoken = "MYINFOWOULDBEHERE";
$accesstokensecret = "MYINFOWOULDBEHERE";
function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
$connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
return $connection;
}
$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
$tweets = $connection->get("https://api.twitter.com/1.1/search/tweets.json?q=".$hashtag);
echo json_encode($tweets);
?>