如何使用PHP来节流API请求 [英] How to throttle API requests using PHP

查看:232
本文介绍了如何使用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,它可以让我们做到这一点一些伪code - 或者是不是真的一样简单迫使实际的API请求功能,1秒中的每个命令之间睡觉?我没有很多的API和大量并发用户的经验,因此任何帮助是AP preciated。

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.

另一个建议是看看像 AMQP 或的 ZMQ ,其中一些也有扩展在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天全站免登陆