dwdawdawdaw
dwdawdawdaw

Reputation: 281

Twitter API - Count number of tweets of a specific string

I'm using the twitter api to try to get an integer that tells me how many tweets there are to a certain string I give.

e.g. I search for "mercedes" and then want to get an integer back from twitter that says: "1249". 1249 would mean that there were so many tweets in the last 2 weeks. Twitter only returns data from the last 2 weeks as far as I know. Because of me it's also okay if I get all records back and pull them by means of php or the like. I have already sent some test requests, but always only get arrays back with a maximum of 20 entries.

Anyone have a solution?

And I already looked at similar questions but couldn't find something that helped me. Many answers in the questions I have seen no longer work, as twitter and its api has changed and evolved

Upvotes: 3

Views: 4602

Answers (2)

Leopoldo Sanczyk
Leopoldo Sanczyk

Reputation: 1609

I couldn't find a solution neither, so I coded it using pieces of code and ideas as the previous @JeffProd one, and avoiding using a lib. I hope it could help you.

PS: You must apply for a Twitter Developer Account and create an app to get your TOKENs and KEYs.

<?php
//Access token & access token secret
define("TOKEN", 'XXXXXXXXXXXXXXXX'); //Access token
define("TOKEN_SECRET", 'XXXXXXXXXXXXXXXX'); //Access token secret
//Consumer API keys
define("CONSUMER_KEY", 'XXXXXXXXXXXXXXXX'); //API key
define("CONSUMER_SECRET", 'XXXXXXXXXXXXXXXX'); //API secret key

$method='GET';
$host='api.twitter.com';
$path='/1.1/search/tweets.json'; //API call path
$url="https://$host$path";
//Query parameters
$query = array(
    'q' => 'wordtosearch',          /* Word to search */
    'count' => '100',               /* Specifies a maximum number of tweets you want to get back, up to 100. As you have 100 API calls per hour only, you want to max it */
    'result_type' => 'recent',      /* Return only the most recent results in the response */
    'include_entities' => 'false'   /* Saving unnecessary data */
);
//time window in hours
define("WINDOW", 1); 

//Authentication
$oauth = array(
    'oauth_consumer_key' => CONSUMER_KEY,
    'oauth_token' => TOKEN,
    'oauth_nonce' => (string)mt_rand(), //A stronger nonce is recommended
    'oauth_timestamp' => time(),
    'oauth_signature_method' => 'HMAC-SHA1',
    'oauth_version' => '1.0'
);
//Used in Twitter's demo
function add_quotes($str) { return '"'.$str.'"'; }

//Searchs Twitter for a word and get a couple of results
function twitter_search($query, $oauth, $url){  
    global $method; 

    $arr=array_merge($oauth, $query); //Combine the values THEN sort
    asort($arr); //Secondary sort (value)
    ksort($arr); //Primary sort (key)
    $querystring=http_build_query($arr,'','&');
    //Mash everything together for the text to hash
    $base_string=$method."&".rawurlencode($url)."&".rawurlencode($querystring);
    //Same with the key
    $key=rawurlencode(CONSUMER_SECRET)."&".rawurlencode(TOKEN_SECRET);
    //Generate the hash
    $signature=rawurlencode(base64_encode(hash_hmac('sha1', $base_string, $key, true)));
    //This time we're using a normal GET query, and we're only encoding the query params (without the oauth params)
    $url=str_replace("&amp;","&",$url."?".http_build_query($query));
    $oauth['oauth_signature'] = $signature; //Don't want to abandon all that work!
    ksort($oauth); //Probably not necessary, but twitter's demo does it
    $oauth=array_map("add_quotes", $oauth); //Also not necessary, but twitter's demo does this too  
    //This is the full value of the Authorization line
    $auth="OAuth ".urldecode(http_build_query($oauth, '', ', '));
    //If you're doing post, you need to skip the GET building above and instead supply query parameters to CURLOPT_POSTFIELDS
    $options=array( CURLOPT_HTTPHEADER => array("Authorization: $auth"),
                      //CURLOPT_POSTFIELDS => $postfields,
                      CURLOPT_HEADER => false,
                      CURLOPT_URL => $url,
                      CURLOPT_RETURNTRANSFER => true,
                      CURLOPT_SSL_VERIFYPEER => false);
    //Query Twitter API
    $feed=curl_init();
    curl_setopt_array($feed, $options);
    $json=curl_exec($feed);
    curl_close($feed);
    //Return decoded response
    return json_decode($json);
};

//Initializing
$done = false;      //Loop flag
$countTweets=0;     //Tweets fetched
$twitter_data = new stdClass();
$now=new DateTime(date('D M j H:i:s O Y')); //Current search time

//Fetching starts
do{
    $twitter_data = twitter_search($query,$oauth,$url);
    //Partial results, updating the total amount of tweets fetched
    $countTweets += count($twitter_data->statuses); 
    //If not all the tweets have been fetched, then redo... 
    if(isset($twitter_data->search_metadata->next_results)){        
        //Parsing information for max_id in tweets fetched
        $string="?max_id="; 
        $parse=explode("&",$twitter_data->search_metadata->next_results);       
        $maxID=substr($parse[0],strpos($parse[0],$string)+strlen($string));     
        $query['max_id'] = -1+$maxID; //Returns results with an ID less than (that is, older than) or equal to the specified ID, to avoid getting the same last tweet
        //Twitter will be queried again, this time with the addition of 'max_id'        
    }else{      
        $done = true;
    }   
}while(!$done);

//If all the tweets have been fetched, then we are done
echo "<p>query: ".urldecode($query['q'])."</p>";
echo "<p>tweets fetched: ".$countTweets."</p>";
?>

Upvotes: 1

JeffProd
JeffProd

Reputation: 3148

Using the public search API, you will get tweets from the last 7 days only and not all tweets. So your results won't be accurate.

If you still want to test, you have to use the standard search API : https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets.html

Set the "cout" parameter to 100, and check the "next_results" value in the results to loop 100 others tweets and so on until you get no result.

Upvotes: 3

Related Questions