Home > Article > Backend Development > Solution to PHP reading remote data timeout
How does PHP solve the timeout problem of reading remote data? This article mainly introduces the solution to the timeout of reading remote data by the PHP file_get_contents function. This article directly gives the solution code. I hope to be helpful.
The file_get_contents function often fails to read remote data when the network condition is poor.
The solution is as follows:
/*设置超时配合失败之后尝试多次读取,效果比原先好很多*/ $url = 'http://www.jb51.net'; $opts = array( 'http'=>array( 'method'=>"GET", 'timeout'=>1, //设置超时 ) ); $context = stream_context_create($opts); $contents = @file_get_contents($url,false,$context); ?>
Related recommendations:
Detailed explanation of php file reading series methods
PHP file reading fread, fgets, fgetc, file_get_contents and file function usage example code
Simple Introduction to PHP file lock and process lock
The above is the detailed content of Solution to PHP reading remote data timeout. For more information, please follow other related articles on the PHP Chinese website!