Home  >  Article  >  Backend Development  >  Several solutions to PHP file_get_contents function timeout_PHP tutorial

Several solutions to PHP file_get_contents function timeout_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:44:421066browse

Here are two simple introductions:
1. Increase the timeout time limit
It should be noted here: set_time_limit only sets the timeout of your PHP program, not the timeout for the file_get_contents function to read the URL. time.
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 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);
fpassthru($fp);

2. If there is a delay, try a few more times
Sometimes the failure is caused by network and other factors. There is no solution, but you can modify the program and try again several times when it fails. If it still fails, give up, because file_get_contents() will return FALSE if it fails, so you can write the code as follows:
Copy code The code is as follows:

$cnt=0;
while($cnt < 3 && ($str=@file_get_contents('http…'))===FALSE) $cnt++;

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320466.htmlTechArticleHere are two simple introductions: 1. Increase the time limit of timeout. Note here: set_time_limit just sets your PHP The timeout of the program, rather than the file_get_contents function reading...
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