简体   繁体   中英

Rate limit. Twitter API

I'm working on a small and simple code which basically does some tweets filtering. The problem is that I'm hitting the request limit of Twitter API and I would like to know if there is a workaround or if what I want to do just cannot be done.

First, I type a twitter username to retrieve the ID's of people this user follows.

$user_id = $_GET["username"]; 
$url_post = "http://api.twitter.com/1/friends/ids.json?cursor=-1&screen_name=" . urlencode($user_id);
$following = file_get_contents($url_post, true);
$json = json_decode($following);

$ids = $json->ids;

Twitter API responds with a list of ID's.

Here comes the problem. The next step is to make a request to find out username, profile picture and description for each one of those ID's.

$following = array();
foreach ($ids as $value)
{
$build_url = 'http://api.twitter.com/1/users/lookup.json?user_id=' . $value . '';
$following[] = $build_url;
}


foreach ($following as $url) 
{
$data_names = file_get_contents($url, true); //getting the file content
$json_names = json_decode($data_names);

foreach ($json_names as $tweet) {

$name = $tweet->name;
$description = $tweet->description;

echo '<p>'; 
echo $name . '<br>';
echo $description;
echo '</p>';
}
}

If the user follows 50 people it works. But if he follows, let's say, 600 hundred, that would be 600 hundred request (for username, description and profile pic) to Twitter API which exceeds the limit.

Is there any way to workaround this o it just cannot be done?

Thank you!

You can and should request users/lookup API endPoint with 100 userIds at a time, instead of doing one request per twitter ID. cf. https://dev.twitter.com/docs/api/1.1/get/users/lookup

You have to replace your forEach loop ( foreach ($following as $url) ) by a recursive function.

At the end of the function, check the number of hits remaining before calling it again (cf. this link to see how to know the time remining until you get rate limited).

If there is no hit left, sleep 15 minutes before calling the function again, otherwise do the call again.

There is plenty of information on how to do this, use Google and search existing stackOverflow questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM