2017-10-04 114 views
1

您好我目前使用twitter API,并且无法获取鸣叫的created_at日期。返回的日期看起来就像我要求API的日期,而不是推文的发布日期。Twitter API搜索Get Tweet created_at时间

我能够得到这个日期,但如果它的日期我称为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日期。

+0

我刚刚试过了,它的返回差异日期,只有第一个是'Wed Oct 04 11:24:19 +0000 2017',你能展示你如何通过推文循环。 –

+0

你只能通过'$ tweetCreatedTime = $ tweets-> statuses [0] - > created_at获得第一条推文的日期,'''''''''''''''' – 3gth

+0

我已编辑我的问题,告诉你我循环使用 –

回答

1

我建议你重写你的代码,你有它的错误"#"+$coinSymbol[$i].是PHP中的字符串连接。好吧,如果你有一个符号数组,你可以使用foreach循环它们,然后抓住你的推特计数。请注意,如果你有100个货币符号,你很快就会受到Twitter的限制。

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

谢谢为了帮助!我真的很感激:) $ coinSymbol数组有大约1000个硬币,我不能要求api每小时超过100次。你有什么建议吗? –

+0

我知道我不应该现在我正在使用多个应用程序,因为我无法找到另一种方式做到这一点,而无需获得企业版。这就是为什么我只使用$ coinSymbol数组的一个百分比,每个应用程序会每个请求100个请求 –

+0

可能最好的办法是将符号分割为极限,然后通过cron作业批量执行并跟踪通过增加数据库中的query_count列来查询等等。无论如何,1k个符号都需要一段时间。 –