Search code examples
phparraystwittercount

Get total count of mentions from a twitter api response


I am using the basic search twitter api, I would like to get the total count of a specific word mentioned in the response. This is what my api call looks like -

$url = "https://api.twitter.com/1.1/search/tweets.json";
$requestMethod = "GET";

// Keyword to search
$getfield = '?q=elrond&count=20';

$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest(),$assoc = TRUE);

if(array_key_exists("errors", $string)) {echo "<h3>Sorry, there was a problem.</h3><p>Twitter returned the following error message:</p><p><em>".$string[errors][0]["message"]."</em></p>";exit();}

echo "<pre>";
    print_r($string);
echo "</pre>";

foreach($string as $array){
    $i++;
}

echo $i; 

When I echo $i I get a count of 2, but if I look at the actual response their is over 100 mentions of the keyword. What method would I use to count the number of times the keyword is in the response?

Here is an example response I get -

[1] => Array
            (
                [created_at] => Tue Aug 11 16:04:39 +0000 2020
                [id] => 1293216771244261381
                [id_str] => 1293216771244261381
                [text] => @Meter_IO @ElrondNetwork You know just the right partnership, with elrond network, you are certainly in for something big. 🥳
                [truncated] => 
                [entities]

I would be searching the [text] field to get the count


Solution

  • You've got some syntax errors here and are missing a definition of i, but the fundamental issue is that you're trying to count the wrong thing.

    If you print_r($string) at the end of your code, you will see that it is returning an array containing 2 items - [statuses] => Array and [search_metadata] => Array. So 2 is the correct output from your script as written.

    What you could do instead, is to count the statuses array itself.

    foreach($string["statuses"] as $array){
        $i++;
    }
    

    The other thing you could do is to look at the [search_metadata] array, which contains the count of the results:

       [search_metadata] => Array
            (
                [completed_in] => 0.161
                [max_id] => 1293225170983772160
                [max_id_str] => 1293225170983772160
                [next_results] => ?max_id=1293218662854402059&q=elrond&count=20&include_entities=1
                [query] => elrond
                [refresh_url] => ?since_id=1293225170983772160&q=elrond&include_entities=1
                [count] => 20
                [since_id] => 0
                [since_id_str] => 0
            )
    

    Although, both of these actually return the number of Tweets, which match the number you requested count=20... so if you want to count the keywords, you'll have to decide which fields(s) in each response Tweet you want to count them from, and then iterate over those entries in each string.