我有以下三个Twitter帖子:
-> Twitter with message 3
-> Twitter with message 2
-> Twitter with message 1
Twitter with message 3
是我的时间轴中的最新时间。
我正在尝试按降序(最新到最旧)的顺序检索1条帖子。我关注的Twitter API文档为https://developer.twitter.com/en/docs/tweets/timelines/api-reference/get-statuses-user_timeline
根据API,我正在尝试实现一些代码逻辑,因此我具有类似这样的功能:
function getPosts($lastTwitterId = null)
{
// I always retrieve 1 at a time (for testing purposes)
$data = ['user_id' => 'my user id', 'count' => 1];
// Get last twitter Id retrieved
if ($lastTwitterId)
$data['max_id'] = $lastTwitterId;
return $twitter->get('statuses/user_timeline', $data);
}
使用此代码,我得到了以下行为:
-> grabs posts
-> Twitter with message 3 -> OK
-> grabs next posts
-> Twitter with message 3 -> NOT OK DUPLICATED (should get -> Twitter with message 2)
-> grabs next posts
-> Twitter with message 3 -> NOT OK DUPLICATED (should get -> Twitter with message 1)
我已经检查过,并且在第一次调用后$lastTwitterId
变量正确获得了值。
已解决。
解决方案在此帖子using since_id and max_id in Twitter API中。我唯一需要更改的是:
$data['max_id'] = $lastTwitterId;
进入此:
$data['max_id'] = $lastTwitterId - 1;