Home > Backend Development > PHP Tutorial > Detailed explanation of timeout processing method of PHP function file_get_contents_PHP Tutorial

Detailed explanation of timeout processing method of PHP function file_get_contents_PHP Tutorial

WBOY
Release: 2016-07-21 15:09:51
Original
962 people have browsed it

1. Increase the time limit of timeout
It should be noted here: set_time_limit only sets the timeout of your PHP program, not the timeout of the file_get_contents function reading the URL. 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);

2. If there is a delay, there will be more Try several times
Sometimes the failure is caused by network and other factors. There is no solution, but you can modify the program. If it fails, try again several times. If it still fails, give up, because file_get_contents() will return FALSE if it fails. So you can write the code as follows:
$cnt=0;
while($cnt < 3 && ($str=@file_get_contents('http...'))===FALSE) $cnt++;
The above method is OK for dealing with timeouts.
Someone discovered 'method'=>"GET", GET can also be set to post, the function is as follows
Copy the code The code is as follows:

function Post($url, $post = null)
{
$context = array();

if (is_array($post)) {
ksort($post); ' content' => http_build_query($post, '', '&'),
              ); > }

$data = array (
'name' => 'test',
'email' => 'test@gmail.com',
'submit' = > 'submit',
);

echo Post('http://www.example.com', $data);






http://www.bkjia.com/PHPjc/327191.html

www.bkjia.com
true
http: //www.bkjia.com/PHPjc/327191.html

TechArticle1. Increase the time limit of timeout. Note here: set_time_limit only sets the timeout of your PHP program, not The timeout period for the file_get_contents function to read the URL. Real cultivation...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template