Faster API REST - PHP

Asked

Viewed 273 times

0

Good morning Stack overflow!!

Next, I’m doing an integration with my bank Sql server of an APIRest, where I pass the information I want in the URL and receive the content as json, however, I perform several requests on this API as I make a SELECT in my bank that returns all the ID’s of the sales opportunities and I pass this ID in the API url, only once in a while there are some errors because the API allows only 100 requests every 10 seconds, I put a counter to perform a sleep, but still the error continues, follows code below:

while($RFP = odbc_fetch_array($result)) {

//contador que coloquei para o limite da requisição da api
   if($contador >= 96){
         $contador = 0;
         sleep(10); // 5 segundos para proxima requisitar da API
   }
   $url = "https://api.pipedrive.com/v1/deals/".$RFP['ID']."/flow?start=0&api_token=meutokenaqui";
   $ch = curl_init();

   curl_setopt($ch, CURLOPT_URL,$url );
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

   $cAlljson = curl_exec($ch);
   // liberar
   curl_close($ch);

   $contador = $contador + 1;  // Contador para limite de requisições da API

   // Decodifica o Json em Objeto
   $cAlljson = json_decode($cAlljson);
   $cAlljson = $cAlljson->data;



   if (is_array($cAlljson)){
   foreach($cAlljson as $e)
     {
  • Good morning, from what I’ve seen of documentation of the API, they return the headers in the header X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset. You can print them every request to debug and see if this is what is causing the errors. Also, if you are not in a hurry to make this charge (from what I understand), I suggest putting a sleep every request (a second suddenly, or a half), not to bombard their API with consecutive reqs (it may be because of this the error, some Flood protection/firewall). But it’s just a hunch.

  • You can also check the header Retry-After: In case the limit is exceeded for the time window, the Pipedrive API will Return an error Response with HTTP code 429 and Retry-After header that will incicate the amount of Seconds before the limit resets.

  • I managed to resolve with the x-ratelimit-limit issue, thank you, I had not attacked myself on that issue.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.