views:

124

answers:

2

I am creating a webapp that relies on following a status timeline from twitter in php

I am making a request to the twitter using the following API call:

$since_id is equal to a stored id based on the last parsed tweet.

$req_url = 'http://twitter.com/statuses/friends_timeline.xml?count=20&page=1&since_id='.$since_id;

I have noticed that if more than 20 tweets come through since the last call i'll get the most recent 20 tweets since the last call and in turn missing those between the $since_id and the current 20.

example:

  1. at 0800 the last tweet parsed was 70001
  2. 50 tweets are made in the 10 min window since the last API call
  3. at 0810 i call to the API to get tweets since 70001
  4. it returns tweets 70031 - 70051

which causes me to miss 70002-70030

Now to the quest: Is there a way to know how many tweets have been made since the last call? If so, is there a way to make sure when I call the API that I am getting the next tweet in line and not the most recent tweets made?

language: PHP5 using: Twitter REST API

+2  A: 

you can set the since_id parameter to the last seen tweet and specify a count of 200. then retrieve the older ones by specifying max_id.

$startId = [last tweet_id]
$tweets = "twitter.com/statuses/friends_timeline.xml?since_id=${istartId}" .
  "&count=200"

while (count($tweets)) {
   insert $tweets into table on duplicate key update none
   $maxId = $tweets[count($tweets) - 1]['id'];
   if ($maxId <= $startId) {
     break;
   }
   $tweets = "twitter.com/statuses/friends_timeline.xml?max_id=${maxId}" .
     "&count=200"
}

there doesn't appear to be a way to begin paginating at a particular id. in the api, you always start with the most recent entries and work your way back in time to the last seen id.

it's possible to search within a date range using the search atom and the operators since,until, and from:

http://apiwiki.twitter.com/Twitter-Search-API-Method%3A-search

but indeed, it's still sorted by time desc, so you would need to scroll back in any case.

jspcal
I am trying to avoid making a bunch of calls to the API at least until I find out that Ive been approved for a rate limit increase or not.the since_id seems to just get the most recent specified number of tweets after that id and not the ones directly after, which is what I need it to do.
Jayrox
requesting 200 at a time would be considered 1 api call. the api basically requires that you start at the last page and continue backwards until you get the page just after the last seen id.
jspcal
`while(true)` would be prettier than `for (;;)`.
Alix Axel
@Alix: you are obsessing over syntactic sugar, but if you need a reason, `while (true)` is slower than `for (;;)` (yes, in php it's slower) besides being a tautology :)
jspcal
jspcal, I'll give this a try tonight. I figured I'd most likely have to go this route, but I was trying to avoid it because of the (current) 150 calls per hour rate limit. I have put in a request to have this limit increased, wish me luck :)
Jayrox
thanks, this is the method i'll be taking.
Jayrox