0

我正在使用原始区块链 api,文档说我可以每 10 秒执行 1 个请求,我如何确保我不会超过这个限制?我更愿意用 php 将它保留在服务器端。感谢您的答复

4

1 回答 1

0

每次调用 API 后,将 10 秒添加到您的内部时间计数器,以了解何时允许下一次调用。

class ApiRequest{

   private $nextRequestTime = time();

   private function allowRequest(){   
      $local_time = now();     
      if($local_time >= $this->nextRequestTime ){
         $this->nextRequestTime = ($local_time + 10);
         return true;
      }
      return false;
   }

   public function doRequest($request){
      if($this->allowRequest()){
         // process the $request...
      }
   }

}

当函数ApiRequest::allowRequest()返回false时,您知道您应该稍后处理请求。

于 2016-03-05T16:16:30.880 回答