Home >Backend Development >PHP Tutorial >Solution to PHP file_get_contents function reading remote data timeout_PHP tutorial
This article mainly introduces the solution to the timeout of the PHP file_get_contents function to read remote data. This article directly gives the solution. Code, friends in need can refer to it
When the network condition is relatively poor, the file_get_contents function often fails to read remote data.
The solution is as follows:
The code is as follows:
/*After setting the timeout and failing to cooperate, I tried reading multiple times, and the effect was much better than before*/
$url = 'http://www.jb51.net';
$opts = array(
'http'=>array(
'method'=>"GET",
'timeout'=>1, //Set timeout
)
);
$context = stream_context_create($opts);
$contents = @file_get_contents($url,false,$context);
?>