【问题标题】:twiiter max_id and since_idtwitter 最大 id 和 since_id
【发布时间】:2015-02-04 10:30:57
【问题描述】:

我正在使用 twitter user_timeline api 来获取用户的所有推文。

它在单个请求中返回 200,但问题是如果推文像 400 或 500,我想获取所有推文

下面是我的代码,输出 200 条推文:

require_once('TwitterAPIExchange.php');

$settings = array(
    'oauth_access_token' => "",
    'oauth_access_token_secret' => "",
    'consumer_key' => "",
    'consumer_secret' => ""
);
$requestMethod = 'GET';
$url1="https://api.twitter.com/1.1/statuses/user_timeline.json";

$sc_name = 'DailyRapPics';
$count ='700';

$getfield = '?screen_name='.$sc_name.'&exclude_replies=true&include_rts=true&contributor_details=false';

$twitter = new TwitterAPIExchange($settings);
$tweets  = $twitter->setGetfield($getfield)->buildOauth($url1, $requestMethod)->performRequest();

$tweetarray = json_decode($tweets);
$l = 0;
foreach($tweetarray as $mytweets){
$l++;
}
echo 'total values->>>>>>>>>'.$l;

当我看到推特时,会有像 since_id,max_id 这样的字段

我如何使用它来获取用户的所有推文少于 3200 的推特限制请帮助我

这里的 max_id 和 since_id 可能是什么值,请指导

【问题讨论】:

  • 这可能没有直接关系,但只是为了您的帮助,您需要使用 max_id 和 since_id 作为“字符串”或浮点数。你会省去很多麻烦。

标签: php api twitter


【解决方案1】:

您的问题是关于如何分页的(因为根据Twitter's user_timeline API,请求参数“count”每个查询最多有 200 个结果),但它允许您获取最后 3200 条推文。

正确的分页方式是在这样的算法中使用推文的 ID:

  1. 初始化变量

    $all_tweets = array();
    $max_tweets_per_response = 200;
    $last_tweet_id = 0;
    $max_tweets_you_want_to_obtain = 666;
    
  2. $tweets = 查询结果与您的操作方式相同,但还添加了参数: count=$max_tweets_per_response & since_id=$last_tweet_id
  3. 解码从 Twitter 收到的响应并将其与包含所有 3200 个结果的变量合并。

    $tweetarray = json_decode($tweets);
    $all_tweets = array_merge($all_tweets, $tweetarray);
    
  4. $last_tweet_id = 您收到的最后一条推文的 ID,现在将是变量 $tweet

    中的 ID李>
  5. 第 2 步开始迭代直到

    count($tweetarray) < $max_tweets_per_response
    or
    count($all_tweets) >= $max_tweets_you_want_to_obtain
    
  6. 对所有推文做任何你想做的事,现在将包含在变量 $all_tweets

    echo 'total values->>>>>>>>>'.count($all_tweets);
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-31
    • 2015-12-07
    • 2016-02-08
    • 2016-04-29
    • 2010-12-28
    • 2011-09-18
    • 1970-01-01
    相关资源
    最近更新 更多