Json API 速率限制
Json API Rate Limiting
example.com/series/title-series-A/ 用wordpress插件缓存了1天,但是当缓存过期并且访问者访问post系列
访问者同时访问url的示例
- 示例。com/series/title-series-A/
- 示例。com/series/title-series-B/
- 示例。com/series/title-series-C/
所以访问者访问的所有系列将同时请求 api,对此有什么解决方案吗?
(在此所有 post 类型 'series' 中都有 api php 请求)
function curl_get_contents($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$agent = array('http' => array('user_agent' => 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0'));
$api = 'https://api.example.com/v3/title-series-here';
$results = curl_get_contents($api, false, stream_context_create($agent));
$results = json_decode($results, true);
并且api.example.com每个请求有4秒的速率限制,如果超过这个就会被阻止
是的,您需要在 curl_get_content() 函数中添加睡眠:
function curl_get_contents($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
sleep(5); // here after the request will sleep 5 seconds to permit the rate limit
curl_close($ch);
return $data;
}
$agent = array('http' => array('user_agent' => 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0'));
$api = 'https://api.example.com/v3/title-series-here';
$results = curl_get_contents($api, false, stream_context_create($agent));
$results = json_decode($results, true);
这是我的解决方案:
- 检查响应 header api,如果 header 响应 429 使用 sleep(5);或死();
if(($httpcode == 429)) { sleep(5); }
- 缓存卷曲结果
example.com/series/title-series-A/ 用wordpress插件缓存了1天,但是当缓存过期并且访问者访问post系列
访问者同时访问url的示例
- 示例。com/series/title-series-A/
- 示例。com/series/title-series-B/
- 示例。com/series/title-series-C/
所以访问者访问的所有系列将同时请求 api,对此有什么解决方案吗?
(在此所有 post 类型 'series' 中都有 api php 请求)
function curl_get_contents($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$agent = array('http' => array('user_agent' => 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0'));
$api = 'https://api.example.com/v3/title-series-here';
$results = curl_get_contents($api, false, stream_context_create($agent));
$results = json_decode($results, true);
并且api.example.com每个请求有4秒的速率限制,如果超过这个就会被阻止
是的,您需要在 curl_get_content() 函数中添加睡眠:
function curl_get_contents($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
sleep(5); // here after the request will sleep 5 seconds to permit the rate limit
curl_close($ch);
return $data;
}
$agent = array('http' => array('user_agent' => 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0'));
$api = 'https://api.example.com/v3/title-series-here';
$results = curl_get_contents($api, false, stream_context_create($agent));
$results = json_decode($results, true);
这是我的解决方案:
- 检查响应 header api,如果 header 响应 429 使用 sleep(5);或死();
if(($httpcode == 429)) { sleep(5); }
- 缓存卷曲结果