mars-php-util / rate-limit-bypass
通过使用代理或API密钥轮换绕过API速率限制
1.0.0
2021-08-27 11:15 UTC
Requires
- php: >=7.3.0
This package is auto-updated.
Last update: 2024-09-27 19:32:36 UTC
README
问题
在与第三方API的速率限制作斗争,他们限制每个IP地址每秒n个请求
预期
轻松调用API,无需
解决方案
使用此包,您将使用
- 通过代理进行IP轮换
- 速率限制器来控制请求速率
安装
您可以通过以下两种方法安装此库
使用composer
此项目使用composer。
composer require mars-php-utils/rate-limit-bypass
手动安装
在以下位置检出代码库
vendor_dev/rate-limiter-bypass
composer.json
"repositories": [
{
"type": "path",
"url": "vendor_dev/rate-limiter-bypass"
}
],
如何使用
假设您正在使用curl
进行请求
function getUser($id) { $curl = curl("https://example-api.com/user/$id"); curl_setopt_array($curl, $your_options); $response = curl_exec($curl); return $response; }
您只需要做以下操作
use RateLimitBypass\IpRateLimitHelper; $max_request_per_second = 10; $proxies = [...]; $rotator = new IpRateLimitHelper($proxies, $max_request_per_second); function getUser($id) { $proxy_opt = $rotator->tap(); $curl = curl("https://example-api.com/user/$id"); $your_options = array_merge_recursive($your_options, $proxy_opt); curl_setopt_array($curl, $your_options); $response = curl_exec($curl); return $response; }
通过API密钥轮换进行限制
use RateLimitBypass\ApiKeyRateLimitHelper; $max_request_per_second = 10; $api_keys = [...]; $rotator = new ApiKeyRateLimitHelper($api_keys, $max_request_per_second); function getUser($id) { $api_key = $rotator->tap(); // api key can be in query string, headers, path, ... depend on the API endpoint $curl = curl("https://example-api.com/user/$id?apiKey=$api_key"); curl_setopt_array($curl, $your_options); $response = curl_exec($curl); return $response; }
本地开发
composer.json
"repositories": [
{
"type": "path",
"url": "vendor_dev/mars-php-utils/rate-limit-bypass"
}
],
然后运行
composer require mars-php-utils/rate-limit-bypass
composer会将vendor/mars-php-utils符号链接到vendor_dev/mars-php-utils