Filehandler cron requires sleep to run without errors

The following code is broken once every 36 hours at random. The correctness of info.txt is guaranteed. Simple print 'test'; or sleep(1); between the lines of $handle and while works around the problem, but why?

 $handle = fopen("http://www.domain.do/info.txt", "r"); while (!feof($handle)) { // [do stuff] } fclose ($handle); 

The code is started by the cron job.

+4
source share
2 answers

Adding sleep () or some delay will give fopen time to retrieve the file from the location ... I suppose that the "random" effect may be traffic on the network, which leads to the fact that the search is a little longer than usual.

You can try using cURL to retrieve data:

 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://www.domain.do/info.txt'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); 

$output will contain the contents of the file, so you need to split the string into an array, possibly using a new string as a separator ... only fortunetelling does not know the contents of the file :)

+2
source

Thank you very much. The work works in the short term without errors with a curl. I will also check this for a long time. In 7 days I will give you feedback.

0
source

Source: https://habr.com/ru/post/1441214/


All Articles