2013-09-26 12 views
5

Ho riscontrato un problema che spiega come restituire un conteggio totale per il numero di volte in cui è stato utilizzato un hashtag su Twitter. In passato ho usato il seguente codice che funzionava ma l'indirizzo "http://search.twitter.com/search.json" è stato ritirato da Twitter. Il vecchio codice era:Numero di hashtag dell'API di Twitter 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; 
?> 

io so che tu sai essere necessario utilizzare un applicazione Twitter autorizzato e avere accesso ad essere in grado di estrarre i dati. Sono stato in grado di configurare l'app e posso estrarre un elenco di dati utilizzando il seguente codice, ma non sono sicuro di come utilizzare tali dati per ottenere un conteggio totale. Qualcuno può aiutarmi a ottenere un totale cambiando il codice che ho o aiutandomi a come dovrei farlo. Ecco il codice che ho che tira i dati hashtag:

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

risposta

4

Ecco un esempio di Twitter 1.1 API fatta da this. Tenetelo a mente:

Si prega di notare che il servizio di ricerca di Twitter e, per estensione, l'API Search non è destinata ad essere una fonte esaustiva di Tweets. Non tutti i I tweet verranno indicizzati o resi disponibili tramite l'interfaccia di ricerca.

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

  1. Scarica twitter-api-php ed estrarlo nella cartella al server
  2. Create a Developer Account and an Application
  3. Creare un file twitter_search.php (nella stessa cartella in cui si dispone di twitter-api-php)
  4. Aggiungi token e chiavi a 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.