限速。 Twitter的API [英] Rate limit. Twitter API

查看:572
本文介绍了限速。 Twitter的API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个小而简单code,基本上做一些鸣叫过滤。问题是,我打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.

首先,我输入Twitter用户名来获取人们的ID的这个用户如下:

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使用的ID列表响应。

Twitter API responds with a list of ID's.

这里谈到的问题。下一步是提出请求,找出用户名,个人资料图片和说明那些ID的每个人。

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>';
}
}

如果用户遵循50人它的作品。但是,如果他如下,让我们说,600百,那将是600百请求(用户名,描述和轮廓PIC),以Twitter的API超过了限制。

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.

有什么办法来解决这个Ø它只是不能做?

感谢您!

推荐答案

您可以并且应该在一个时间要求用户/查找 API端点100的用户id,而不是做每个微博ID一个请求。比照 https://dev.twitter.com/docs/api/1.1/get /用户/查找

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

您必须更换forEach循环(的foreach($以下为$ URL))由递归函数。

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).

如果没有击中左,再调用函数睡觉前15分钟,否则再次进行呼叫。

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

有足够的信息,如何做到这一点,使用谷歌搜索和StackOverflow上存在的问题。

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

这篇关于限速。 Twitter的API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆