【发布时间】:2010-12-23 18:53:05
【问题描述】:
如果我要调用用户关注者,使用 twitter API(和 OAuth),(状态/关注者)我将只返回 99 个结果。
有没有办法我可以返回 99,然后从关注者 100 开始再次调用,然后循环这种调用方式,直到返回关注者总数?
还是只返回所有关注者?
【问题讨论】:
如果我要调用用户关注者,使用 twitter API(和 OAuth),(状态/关注者)我将只返回 99 个结果。
有没有办法我可以返回 99,然后从关注者 100 开始再次调用,然后循环这种调用方式,直到返回关注者总数?
还是只返回所有关注者?
【问题讨论】:
<?php
$trends_url = "http://api.twitter.com/1/statuses/followers/fawadghafoor.json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $trends_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curlout = curl_exec($ch);
curl_close($ch);
$response = json_decode($curlout, true);
foreach($response as $friends){
$thumb = $friends['profile_image_url'];
$url = $friends['screen_name'];
$name = $friends['name'];
?>
<a title="<?php echo $name;?>" href="http://www.twitter.com/<?php echo $url;?>"><img class="photo-img" src="<?php echo $thumb?>" border="0" alt="" width="40" /></a>
<?php } ?>
【讨论】:
Twitter API 限制我们对方法关注者/ID 的 api 调用是每 15 分钟 15 个请求。如果你做的比这个 api 多,就会给你一个 Rate Limit Reached 的错误信息。
更多关于twitter API rate Limit的信息 访问-https://dev.twitter.com/docs/rate-limiting/1.1和https://dev.twitter.com/docs/rate-limiting/1.1/limits
【讨论】:
$cursor = -1;
$account_from = 'twitter_account';
do
{
$json = file_get_contents('http://api.twitter.com/1/statuses/followers/' . $account_from .'json?cursor=' . $cursor);
$accounts = json_decode($json);
foreach ($accounts->users as $account)
{
array(
':twitter_id' => $account->id_str,
':account' => $account->screen_name,
':description' => $account->description,
);
}
$cursor = $accounts->next_cursor;
}
while ($cursor > 0);
【讨论】:
虽然我很久以前就问过这个问题,但最近我又开始构建非常相似的东西(+ 新的编程技能)。
我注意到 Twitter API 有一种方法可以在一个请求中获取所有用户的关注者(或关注者)用户 ID。我发现最好的方法是 array_chunk 将 ID 分成 100 个批次(并且只取前 30 个数组,因为我不想在那一小时使用所有用户的 api 请求——他们可能真的想发推文!)。然后有一种方法可以让您获取多达 100 个用户的用户信息(从当前经过身份验证的用户的角度来看),所以我只需执行一个循环(中间稍微睡一会儿),然后您就有 30,000 个推特关注者!
我建议在队列系统中异步执行此操作,就像您在用户请求站点上的页面时动态执行此操作一样,它可能非常慢并且您可能容易出现 HTTP 超时。还要像地狱一样缓存它们!
对不起,我没有发布任何代码,但希望这个思考过程对某人有所帮助:)
【讨论】:
Twitter 每小时只允许一定数量的 API 请求,我认为分钟。您一次可能无法检索超过 99 个请求。
【讨论】:
确保您使用的是正确的调用方式。 followers/ids 一次给你 5000 个(但它只是一个 id 列表)。此调用也使用光标让您逐步浏览用户页面。当你拥有它们时,你会得到一个零回报。
【讨论】:
您需要按照in the API documrnation 的说明指定游标参数。例如。指定 cursor=-1 来请求第一页,然后使用第一个响应中返回的 next_cursor 值:
http://twitter.com/statuses/followers/barackobama.xml?cursor=-1
http://twitter.com/statuses/followers/barackobama.xml?cursor=1300794057949944903
【讨论】: