php 如何在没有限制率问题的情况下使用 instagram API 关注用户?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21393784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I follow users using the instagram API without the limit rate issue?
提问by Sepehrooo
I want to follow a list of user ids in instagram. I made the list and when I try to send the follow request using the api, I get this error: Client request limit reached (code: 400)
我想关注 Instagram 中的用户 ID 列表。我列出了列表,当我尝试使用 api 发送关注请求时,出现此错误:达到客户端请求限制(代码:400)
The developer documentation says: "You are limited to 5000 requests per hour per access_token or client_id overall."
开发人员文档说:“每个 access_token 或 client_id 每小时限制为 5000 个请求。”
but when I get this error, my code hardly followed 50 user ids!
但是当我收到这个错误时,我的代码几乎没有跟踪 50 个用户 ID!
And one more thing, when I got this error I tried to follow people on the instagram app directy (not using the api) and there were no issues!
还有一件事,当我收到此错误时,我尝试在 Instagram 应用程序目录上关注人们(不使用 api),但没有任何问题!
This is some part of my code that get's this error:
这是我的代码的一部分,它得到了这个错误:
foreach ($usersArr as $user) {
$i++;
$url = 'https://api.instagram.com/v1/users/'.$user->id.'/relationship?access_token='. $accessToken;
$data = 'action=follow';
$resultObj = json_decode(sendPostData($url, $data));
$responseCode = $resultObj->meta->code;
$errorMsg = $resultObj->meta->error_message;
if ($responseCode != 200)
echo "\"" . $errorMsg . "\" AT " . date('Y-m-d H:i:s') . "\n\n";
while ($responseCode != 200){
sleep(60);
$resultObj = json_decode(sendPostData($url, $data));
$responseCode = $resultObj->meta->code;
if ($responseCode != 200) {
$errorMsg = $resultObj->meta->error_message;
echo "STILL \"" . $errorMsg . "\" AT " . date('Y-m-d H:i:s') . "\n\n";
}
elseif($responseCode == 200){
echo "Limit Finished At: " . date('Y-m-d H:i:s') . "\n\n";
break;
}
}
echo "User NO. " .$i . " has been followed!" . "\n\n";
}
Thanks a lot :)
非常感谢 :)
采纳答案by Rijndael
Note, the API is complaining about requests, not follows. You may be following 50 users but you have made API requests that exceed 5000. With your current code, it will only take 100 page refreshes for you to reach that limit. Might sound like a lot of refreshes but if you are debugging/developing code then it is plausible to assume that you could exceed the 100 refresh threshold with relative ease.
请注意,API 抱怨的是requests,而不是 follow 。您可能正在关注 50 个用户,但您发出的 API 请求超过了 5000。使用您当前的代码,您只需刷新 100 次页面即可达到该限制。可能听起来像很多刷新,但如果您正在调试/开发代码,那么假设您可以相对轻松地超过 100 次刷新阈值是合理的。
To mitigate this problem, if you are still working/debugging this code then reduce the number of follows from 50 to 1 or 2. Once your code is working then reinstate the previous number of follows. I would recommend you cache the return objects too, for 5 or 10 minutes. That way you won't need to worry about exceeding the API limits.
为了缓解这个问题,如果您仍在工作/调试此代码,则将关注次数从 50 减少到 1 或 2。一旦您的代码正常工作,则恢复之前的关注次数。我建议您也将返回对象缓存 5 或 10 分钟。这样您就无需担心超出 API 限制。
回答by mykel
My experience with the relationship endpoint of the API is that you are limited to around 160 requests per hour. This appears to be shared with all the actions (follow/unfollow/block/unblock/approve/deny). I have been unable to find anything that allows you to determine in advance how many requests you have left before you exceed your limit (unlike the X-Ratelimit-Remaining header in for the overall request limit), and I have noticed that a any attempt to use the endpoint is added to your request count, whether or not it succeeds.
我对 API 关系端点的经验是,每小时最多只能处理 160 个请求。这似乎与所有操作(关注/取消关注/阻止/取消阻止/批准/拒绝)共享。我一直无法找到任何可以让您提前确定在超出限制之前还剩下多少请求的内容(与总体请求限制中的 X-Ratelimit-Remaining 标头不同),并且我注意到任何尝试使用端点会添加到您的请求计数中,无论它是否成功。
Because my particular app is processing for multiple people concurrently, I process in small chunks (around 50 requests) and when I finish a chunk, or if I hit a rate limit within the chunk, I move on to another user for a while (typically around 10-15 minutes) before coming back.
因为我的特定应用程序同时为多人处理,我以小块(大约 50 个请求)处理,当我完成一个块时,或者如果我在块内达到速率限制,我会转移到另一个用户一段时间(通常大约 10-15 分钟),然后再回来。
I imagine the API limit is per-appid/clientkey, which is why you could follow users on the instagram app despite reaching your limit.
我想 API 限制是 per-appid/clientkey,这就是为什么尽管达到了限制,您仍然可以在 Instagram 应用程序上关注用户。

