Why function does not work properly?

API website, if accessed (get request) returns the data. If 1 request, all right.
If many requests in a row (10-15), with approximately 15 data request has not come.
A minute later, everything works again.
The website adjusts the load, number of requests per IP over time.

I need to perform a lot of queries. For this I wrote a function.

set_time_limit(1200); //20 minutes to execute the script

$data=get_exec_guaranteed('api.site.ru',180,20); //function Call

//Function
get_exec_guaranteed function($url,$execution_time,$pause_time)
{
$response='error'; //By default, return the word error ie error.
$response_time=time()+$execution_time; //Time when the cycle stops, regardless of the result.
//Use usleep in a loop (time in microseconds) to sleep in cycles not working
do {$data=file_get_contents($url); if (!$data) {usleep($pause_time*1000000);} } while (!$data and $response_time>time());
if ($data) $response=$data;
return $response; //Return data or an error or
}


But the function does not work correctly.
The first failure Warning: file_get_contents...failed to open stream: HTTP request failed! HTTP/1.1 429 Too Many Requests in... don't go any further attempts to receive data. Outputs from error. Ie tried to receive data once (the loop?).
And then the cycle is running. Are 4 errors in a row, Warning: file_get_contents...failed to open data stream and come again. Already trying to get data many times (not out of the loop?).

A total of 70 queries, no data 16 and 58 request. Because these requests were only 1 attempt to obtain the data.
And there must be at least 9 (180/20=9 loop).

I.e. output something like this
data=1
data=2
data=3
Warning: file_get_contents //here it is 1 failure of the site because the above has been a lot of requests
data=error //i.e. only 1 attempt?
Warning: file_get_contents //and now we can see that the cycle works
Warning: file_get_contents
Warning: file_get_contents
Warning: file_get_contents
data=5 //getting back the data because it took 4*20=80 seconds and the site again gives me information
data=6
data=7
Warning: file_get_contents
data=error
Warning: file_get_contents
Warning: file_get_contents
Warning: file_get_contents
Warning: file_get_contents
data=9
data=10

Why function does not work properly? (I suspect a bug in the logic.)
June 3rd 19 at 19:25
1 answer
June 3rd 19 at 19:27
The program does not do what the programmer wants, and what he wrote. You have nowhere written that the loop should execute five times. If the file_get_contents request a response back in 2:40 or later, then the loop be executed only once.
And learn code to execute, that is. Read PSR-2, put phpcs.
You have nowhere written that the loop should execute five times

There is an implementation cycle. Data will either be received or not.
If not - wait for 20 seconds again and repeat again.
The cycle is 180 seconds.
The final failure (error) = guaranteed performance cycle around 9 times. 9*20=180.
Each failed attempt is a error Warning: file_get_contents
But the error, before the final failure only one.
Conclusion - there was only one attempt. Question - where's the other 8? - johnnie55 commented on June 3rd 19 at 19:30
Only you somehow believe that file_get_contents returns the result immediately. Imagine that it was carried out 10 minutes and returned false, as it will change the logic of your function? - Kenyatta commented on June 3rd 19 at 19:33
Only reason you believe that file_get_contents returns the result immediately

Do. It is not known how long the server will run their script. And when he answers.

Slightly changed code
echo'/////////////.<br>'; 
echo file_get_contents start Time='.time().'<br>'; 
$data=file_get_contents($url); if (!$data) {usleep($pause_time*1000000);} 
echo end Time file_get_contents='.time().'<br>'; 
echo'/////////////.<br>';


Where to get the data
/////////////.
File_get_contents start time=1532174118
Warning: file_get_contents failed to open stream: HTTP request failed! HTTP/1.1 429 Too Many Requests in
Time of completion file_get_contents=1532174138
/////////////.
Logical. As in the code. Bring the time to try to get the data, display the time.

Where is the error
/////////////.
File_get_contents start time=1532174117
Time of completion file_get_contents=1532174118
/////////////.
Warning: file_get_contents

It is not logical. But as this Warning climbed beyond /////////////? - johnnie55 commented on June 3rd 19 at 19:36

Find more questions by tags PHP