Home >Backend Development >PHP Tutorial >file_get_contents Several solutions to the timeout of the PHP file_get_contents function
Here are two brief introductions:
1. Increase the time limit of timeout
Note here: set_time_limit only sets the timeout of your PHP program, not the timeout of the file_get_contents function reading the URL.
I initially thought that set_time_limit could also affect file_get_contents, but after testing, it was invalid. To truly modify the file_get_contents delay, you can use the timeout parameter of resource $context:
Copy the code The code is as follows:
$opts = array(
'http'=>array(
'method'=> "GET",
'timeout'=>60,
)
);
$context = stream_context_create($opts);
$html =file_get_contents('http://www.example.com', false, $context ; If it still fails, give up, because file_get_contents() will return FALSE if it fails, so you can write the code as follows:
$cnt=0; while($cnt < 3 && ($str=@file_get_contents('http...'))===FALSE) $cnt++;
The above introduces several solutions to the file_get_contents PHP file_get_contents function timeout, including the content of file_get_contents. I hope it will be helpful to friends who are interested in PHP tutorials.