Home > Article > Backend Development > Solution to the timeout of PHP file_get_contents function reading remote data, c function timeout_PHP tutorial
When the network condition is relatively poor, the file_get_contents function often fails to read remote data.
The solution is as follows:
Copy code The code is as follows:
/*After setting the timeout and failing to cooperate, try reading multiple times, the effect is much better than before*/
$url = 'http://www.bkjia.com';
$opts = array(
'http'=>array(
'method'=>"GET",
'timeout'=>1, //Set timeout
)
);
$context = stream_context_create($opts);
$contents = @file_get_contents($url,false,$context);
?>