简体   繁体   中英

Twitter API Search Get Tweet created_at Time

Hi I am currently using the twitter API and I am having trouble getting the created_at date for the tweet. The date which is being returned seems like the date when I requested the API, not the date the tweet was made.

I am able to get this date but if its the date I called the API it has no use to me. The API returned 100 tweets and every single created_at date was the exact same?

This is the code I have written:

$connection = new TwitterOAuth($consumer_key, $consumer_secret, $access_token, $access_token_secret);
$content = $connection->get("account/verify_credentials");
$tweets = $connection->get("search/tweets", ["q" => "#TRIG", "result_type" => "recent", "count" => 100]);
print_r($tweets);

I am using this line of code to get the created_at date:

$tweetCreatedTime = $tweets->statuses[0]->created_at;

This is all the code I am using to count the tweets:

$timeHourAgo = time() - 3600;
$count = 0;
$tweetCoinCount = [];

for ($i=0; $i < round((count($coinSymbol) * 0.06)); $i++) { 
$tweets = $connection->get("search/tweets", ["q" => "#" + $coinSymbol[$i], "result_type" => "recent", "count" => 100]);
$tweetsCount = count($tweets->statuses);
for ($t=0; $t < $tweetsCount; $t++) { 
    $tweetCreatedTime = $tweets->statuses[$t]->created_at;
    $tweetCreatedTimestamp = strtotime($tweetCreatedTime);
    if ($tweetCreatedTimestamp > $timeHourAgo) {
        $count = $count + 1;
    }
}
$tweetCoinCount[$coinSymbol[$i]] = $count;
$count = 0;
}

$coinSymbol is just a string of different currencies which I pull in using another API

all the tweets that are returned have the same created_at date.

I suggest you rewrite your code, you have errors like "#"+$coinSymbol[$i] in it. . is string concatenation in PHP.

Ok, so if you have an array of symbols, you can then loop over them using a foreach and then grab your tweet count. Be aware that if you got 100s of currency symbols you're quickly hit twitters API limit.

$timeHourAgo = time() - 3600;
$tweetCoinCount = [];
$coinSymbol = [
    'TRIG',
    'BTC',
    'ETH'
];

foreach ($coinSymbol as $symbol) {
    $tweets = $connection->get("search/tweets", ["q" => '#'.$symbol, "result_type" => "recent", "count" => 100]);

    $tweetCoinCount[$symbol] = 0;
    foreach ($tweets->statuses as $tweet) {
        if (strtotime($tweet->created_at) > $timeHourAgo) {
            $tweetCoinCount[$symbol]++;
        }
    }
}

print_r($tweetCoinCount);
Array
(
    [TRIG] => 0
    [BTC] => 15
    [ETH] => 15
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM