簡體   English   中英

Twitter API 搜索 獲取 Tweet created_at Time

[英]Twitter API Search Get Tweet created_at Time

您好,我目前正在使用 twitter API,但無法獲取推文的 created_at 日期。 返回的日期似乎是我請求 API 的日期,而不是發布推文的日期。

我能夠得到這個日期,但如果它是我調用 API 的日期,它對我沒有用。 API 返回了 100 條推文,並且每個 created_at 日期都完全相同?

這是我寫的代碼:

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

我正在使用這行代碼來獲取 created_at 日期:

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

這是我用來計算推文的所有代碼:

$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 只是我使用另一個 API 引入的一系列不同貨幣

返回的所有推文都具有相同的 created_at 日期。

我建議你重寫你的代碼,你有像"#"+$coinSymbol[$i]這樣的錯誤。 . 是 PHP 中的字符串連接。

好的,所以如果你有一個符號數組,你可以使用 foreach 遍歷它們,然后獲取你的推文計數。 請注意,如果您有 100 個貨幣符號,您很快就會達到 twitters API 限制。

$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
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM