Reputation: 40
I'm working on a project using an api, my subscription allows me 2 requests per second.
This API allows you to find words that are related to another word.
My project is to use this API recursively. the word A gives the word B and C words B and C each give 2 words, etc., recursively, on a limit that the user specifies.
I store each word in a "word" object. In the __construct of this object, I call the function which calls this API, and which transforms each result into a new object.
Everything works, the only concern is that when the specified recursivity layer is too high, the API doesn't return anything, even though I have a timer of 0.5 seconds.
So I set up a loop. As long as the response to my cURL request is not a 200, I replay it, with a timer of 0.5 seconds.
$a = 200;
$httpcode = 0;
while ($httpcode !== $a) {
$postRequest = [
'content' => $this->getMot(),
'lang' => 'fr',
'limit' => $this->getLimite(),
'key' => "//////////////////////"
];
$cURLConnection = curl_init('https://api.keywords.gg/entities');
curl_setopt($cURLConnection, CURLOPT_HTTPHEADER,
['Content-Type: application/json']);
curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, json_encode($postRequest));
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
$apiResponse = curl_exec($cURLConnection);
$httpcode = curl_getinfo($cURLConnection, CURLINFO_HTTP_CODE);
$apiResponse = curl_exec($cURLConnection);
curl_close($cURLConnection);
}
I think my throttling method is wrong. Could you give me some advice?
Upvotes: 0
Views: 476
Reputation: 1974
As you can see in the sleep documentation it expects an integer.
You send a floating number 0.6 then cast to the integer value 0.
So your sleep call become:
sleep(0)
You need to use usleep(int microseconds) if you need more precision.
usleep(600)
Upvotes: 1