Home  >  Article  >  Backend Development  >  Solution to PHP reading remote data timeout

Solution to PHP reading remote data timeout

*文
*文Original
2017-12-27 10:15:241618browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Type constraints in PHPNext article:Type constraints in PHP