api
  • twitter
  • hashtag
  • 2013-09-26 17 views 5 likes 
    5

    Twitterでハッシュタグが使用された回数の合計数を返す方法がわかりました。過去に私は働いていたが、その後 "http://search.twitter.com/search.json"というアドレスがTwitterによって引退したという次のコードを使用した。古いコードは: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; 
    ?> 
    

    私はあなたが認可されたツイッターアプリケーションを使用し、データを引き出すためにアクセスできることがわかっています。私は、アプリケーションを設定することができたと私は、次のコードを使用してデータのリストを取得することができますが、私はどのように合計カウントを思い付くためにそのデータを使用するか分からない。誰かが、私が持っているコードを変更するか、私がそれについてどうやってやるべきかを教えて、合計を得るのを手伝ってもらえますか?ここでハッシュタグのデータを引き出し、私が持っているコードです:

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

    はこちらthisから作られたTwitterの1.1 APIのための一例です。この点に注意してください:

    拡張することにより、Twitterの検索サービスとは、 検索APIは、ツイートを網羅源であることを意味するものではないことに注意してください。 すべてではありません ツイートのインデックスは、検索インターフェイスを介してとなります。

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

    1. ダウンロードtwitter-api-phpとあなたはさえずり-API-PHPを持っている同じフォルダ内のファイルtwitter_search.phpを(作成してサーバー
    2. Create a Developer Account and an Application
    3. であなたのフォルダに解凍)
    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.

    関連する問題