如何使用 PHP 限制 API 请求 [英] How to throttle API requests using PHP

查看:20
本文介绍了如何使用 PHP 限制 API 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们计划使用 SEMrush API,它允许访问与域名和搜索关键字相关的 SEO 数据.根据他们的使用条款,他们限制使用以避免杀死他们的服务器:

We plan to use the SEMrush API, which allows access to SEO data relating to domain names and search keywords. Under their Terms of Use, they limit their usage to avoid killing their servers:

您每秒执行的请求不得超过 10 个,同时执行的请求不得超过 2 个.

You may not perform more than 10 requests per second, nor more than 2 simultaneous requests.

我们将在 PHP 中构建一个简单的工具,用于根据域名聚合数据,并正在寻找有关如何满足该要求的基础知识.我们正在为成百上千的潜在用户进行规划.

We are going to be building a simple tool in PHP that aggregates data based on a domain name and are looking for the basics on how to fulfill that requirement. We are planning for hundreds/thousands of potential simultaneous users.

也许有人可以在 PHP 中提供一些伪代码让我们这样做 - 或者它真的像强制实际的 API 请求函数在每个命令之间休眠 1 秒一样简单吗?我在 API 和大量并发用户方面没有太多经验,因此感谢您提供任何帮助.

Maybe someone can provide some pseudo code in PHP that would let us do this - or is it really just as simple as forcing the actual API request function to sleep for 1 second in between each command? I don't have a lot of experience with APIs and large amounts of concurrent users so any help is appreciated.

推荐答案

PHP 确实不是用于并发编程的最佳语言.但是,您可以将一些第三方解决方案与 PHP 一起使用,以帮助您实现目标.

PHP is really not the best language to use for concurrent programming. However, there are some third party solutions that you can use along-side of PHP that can help you achieve your goals.

您需要的是可以为您处理实际请求的作业管理器或队列系统.由于这是一个后端工具(至少这是我从您的问题中收集到的),因此它不需要 PHP 来处理对作业本身的实际控制,而只需要一些控制流程来安排这些单个作业并将它们交给您的 PHP 脚本,以便您可以有效地施加这些限制.

What you need is a job-manager or a queue system that can handle the actual requests for you. Since this is a back-end tool (at least that's what I gathered from your question) it doesn't require PHP to handle the actual control over the jobs themselves, but just have some controlling process schedule these individual jobs and hand them to your PHP scripts so that you can effectively impose these limits.

我的第一个建议是尝试诸如 gearman 之类的东西,它是一位出色的工作经理,并且拥有 PHP 中的扩展 以帮助您与库交互.

My first suggestion would be to try something like gearman, which is a great job manager and has an extension in PHP to help you interface with the library.

另一个建议是查看队列系统,例如 amqpzmq,其中一些还有 PHP 中的扩展.

Another suggestion is to take a look at queue systems like amqp or zmq, some of which also have extensions in PHP.

这里有一个示例场景供您使用...

So here's an example scenario for you...

您有一个 PHP 脚本,可以接受这些请求并将它们传递给您的作业管理器或通过套接字排队.作业管理器或队列将存储请求并将其分发给各个工作人员,这种方式可以集中和控制以施加这些限制.我给你的链接中有一些例子可以帮助你到达那里.然而,在没有这些工具帮助的情况下,仅在 PHP 中执行此操作将被证明是相当棘手的,如果不精心设计和考虑,最终可能会出现一些非常边缘情况下的错误行为.

You have a PHP script that accepts these requests and hands them off to your job manager or queue over a socket. The job manager or queue will store the request and distribute it off to the individual workers in an a way that can be centralized and controlled to impose these limits. There are some examples from the links I gave you that can help you get there. However, doing it purely in PHP without the aid of these tools will prove quite tricky and could wind up in some very edge-case buggy behavior if not carefully crafted and considered.

这篇关于如何使用 PHP 限制 API 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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