Search code examples
resttwitter

Correct way to fetch user information with twitter API?


I know I can use the http://apiwiki.twitter.com/w/page/24142947/Twitter-REST-API-Method:-users-lookup method to look up the user info of a person but what if I have to load a large amount of users? Say for example, 40-50 users at a time. Do I call that method 40-50 times? That seems a bit harsh considering there's a limit to API and it'll block the user from using their own account.

Is there a better way?


Solution

  • First of all, you should be using the newer developer site at dev.twitter.com for your docs. The page you need is: https://dev.twitter.com/rest/reference/get/users/lookup

    This API call accepts a string of up to 100 user_ids separated by commas: http://api.twitter.com/1/users/lookup.json?user_id=783214,6253282

    You get back a JSON object containing a list of users. In PHP you would use json_decode() to use this data, and then a foreach() loop to step through each user object.

    The results come back in random order (NOT in the order you asked for), so you have to step through each user, and add them to a database or array for further processing.

    One of the big gotchas is that users that have been deleted or suspended will not have any data returned, so you have to compare the user_ids of the results to those you requested to figure out which weren't returned.

    At 100 users per call, and 350 API calls per hour, you can get the complete account info for 35,000 users per hour. If you get users you need data about spread out over time, you can collect them into a single batch, and send it out when you have 100 accumulated.