2013-09-26 72 views
5

我有一个问题,弄清楚如何返回总数在Twitter上使用hashtag的次数。在过去,我使用了以下代码,但这些地址已经被Twitter推出了,但“http://search.twitter.com/search.json”地址已被撤销。旧代码是:Twitter API 1.1标签计数

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

回答

4

这里是Twitter的API 1.1从this做一个例子。请记住:

请注意,Twitter的搜索服务以及扩展名为 的搜索API并不意味着是推文的详尽来源。 并非所有 推文将通过搜索界面索引或提供

https://dev.twitter.com/docs/api/1.1/get/search/tweets

  1. 下载twitter-api-php并将其解压到您的文件夹在您的服务器
  2. Create a Developer Account and an Application
  3. 创建一个文件twitter_search.php(在同一文件夹,你必须Twitter的API-PHP
  4. 将您的代币和钥匙添加到twitter_search.php

twitter_search.php:

<?php 
require_once('TwitterAPIExchange.php'); 

/** Set access tokens here - see: https://dev.twitter.com/apps/ **/ 
$settings = array(
    'oauth_access_token' => "YOUR_OAUTH_ACCESS_TOKEN", 
    'oauth_access_token_secret' => "YOUR_OAUTH_ACCESS_TOKEN_SECRET", 
    'consumer_key' => "YOUR_CONSUMER_KEY", 
    'consumer_secret' => "YOUR_CONSUMER_SECRET" 
); 

/** Note: Set the GET field BEFORE calling buildOauth(); **/ 
$url = 'https://api.twitter.com/1.1/search/tweets.json'; 
$requestMethod = 'GET'; 
$hashtag = 'twitterapi'; 
$getfield = '?q=%23'.$hashtag.'&result_type=recent&include_entities=true&count=100'; 

// Perform the request 
$twitter = new TwitterAPIExchange($settings); 
$json_output = $twitter->setGetfield($getfield) 
      ->buildOauth($url, $requestMethod) 
      ->performRequest(); 

// Let's calculate how many results we got 
$json = json_decode($json_output); 
$n = count($json->statuses); 
echo $n; 
?> 

Using the Twitter Search API

I posted originally this example in here.