How does the PHPfile_get_contents function time out? When using the file_get_contents function, timeout often occurs. here, you need to check the error message to see which error occurs. the more common is read timeout, in this case, you can try to avoid or solve the problem. Here we will briefly introduce two types :? 1. How long does the timeout limit increase? Note: several solutions for set_time PHP file_get_contents function timeout
?
When using the file_get_contents function, timeout often occurs. here, you need to check the error message to see which error occurs. the more common is read timeout, in this case, you can try to avoid or solve the problem.
Here we will briefly introduce two types :?
1. How long does the timeout limit increase?
Note: set_time_limit only sets the timeout time for your PHP program, rather than the timeout time for the file_get_contents function to read the URL .?
At first, I thought that set_time_limit could also affect file_get_contents. after testing, it was invalid. You can use the timeout parameter of resource $ context to modify the file_get_contents latency :?
$opts = array( ‘http'=>array( ‘method'=>”GET”, ‘timeout'=>60, ) ); $context = stream_context_create($opts); $html =file_get_contents('http://www.example.com', false, $context); fpassthru($fp); ?
2. how many times should I try again if there is a latency?
Sometimes the failure is caused by factors such as the network and there is no solution, but you can modify the program and retry several times when the failure occurs. if the failure still fails, the program will be abandoned, because if file_get_contents () fails, FALSE will be returned, so you can write the code as follows :?
$cnt=0; while($cnt < 3 && ($str=@file_get_contents('http…'))===FALSE) $cnt++; ?
?