douchan7552 2014-12-06 08:15
浏览 40
已采纳

twitter max_id和since_id

i am using twitter user_timeline api to get all tweets of a user .

it returns 200 in single request but problem is that i want to get all tweets if tweets are like 400 or 500

below is my code that give 200 tweets in output :

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;

when i see twitter there are field like since_id,max_id

how can i use it to get all tweet of user less than 3200 of twitter limit please help me

what could be value od max_id and since_id here please guide

  • 写回答

1条回答 默认 最新

  • dtpfia3334 2015-03-13 10:58
    关注

    Your question regards to how to paginate (since the request param "count" has a maximum of 200 results per query according to Twitter's user_timeline API), but it allows you to get the last 3200 tweets.

    The right way to paginate it is with the ids of the tweets in an algorithm like this:

    1. Initialise variables

      $all_tweets = array();
      $max_tweets_per_response = 200;
      $last_tweet_id = 0;
      $max_tweets_you_want_to_obtain = 666;
      
    2. $tweets = result of the query the same way you are doing it but also adding the params: count=$max_tweets_per_response & since_id=$last_tweet_id
    3. Decode response received from Twitter and merge it with the variable that will contain all 3200 results.

      $tweetarray = json_decode($tweets);
      $all_tweets = array_merge($all_tweets, $tweetarray);
      
    4. $last_tweet_id = the ID of the LAST tweet you received, that right now will be the one in the variable $tweet

    5. Iterate from step 2 onwards until

      count($tweetarray) < $max_tweets_per_response
      or
      count($all_tweets) >= $max_tweets_you_want_to_obtain
      
    6. Do whatever you need with all your tweets, that will now be contained in variable $all_tweets

      echo 'total values->>>>>>>>>'.count($all_tweets);
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思