Home >Backend Development >PHP Tutorial >PHP file_get_contents set timeout processing method_PHP tutorial

PHP file_get_contents set timeout processing method_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:13:20842browse

Timeout handling of file_get_contents

In other words, starting from PHP5, file_get_content already supports context (the manual says: 5.0.0 Added the context support.), that is to say, starting from 5.0, file_get_contents can actually POST data.

The article I am talking about today is about timeout. Indeed, when submitting across servers, you will inevitably encounter a timeout. What should you do at this time? set_time_limit is useless, it can only be controlled by the timeout time in the context. On the contrary, we are not to suppress but to manage. For example, after a timeout returns an error, make an attempt, just like settimeout in js, to reprocess the function. After more than 3 or 5 errors, we will definitely think that we cannot connect to the server and give up completely. This is a good method and should be recommended. actually. Not all file_get_contents, as long as it supports context, it should be added to avoid timeout and waste of time. The functions that can be supported in this way are roughly: fsocketopen (the last parameter of this function. It seems to be more recommended to use the stream_time_out function for control when reading the stream), fopen (context support is also added starting from PHP5), file (support is added in PHP5 ), curl (curl has its own variable CURLOPT_TIMEOUT), etc.

When using the file_get_contents function, timeouts often occur. Here you need to check the error message to see what kind of error it is. The more common one is read timeout. In this case, you can use some methods to solve it. Try to avoid or solve it as much as possible. Here is a brief introduction to two types:

1. Increase the time limit for timeout

Note here: set_time_limit only sets the timeout of your PHP program, not the timeout of the file_get_contents function reading the URL.

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'=>1,//unit seconds
)
);

$cnt=0;
while ($cnt<3 && ($bb=file_get_contents("http://www.jb51.net", false, stream_context_create($opts)))===FALSE) $cnt++;
echo $cnt;
echo $bb;

2. If there is a delay once, 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. 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 :

Copy code The code is as follows:

$cnt=0;
while($cnt<3 && ($bb =file_get_contents("http://www.jb51.net", false, stream_context_create($opts)))===FALSE) $cnt++;

The above method is OK for dealing with timeouts. What about Post? Be careful and someone discovered 'method'=>"GET", yes! Can it be set to post? I searched for relevant information on Baidu, and it’s really good! And someone has written a copycat version of the post value function, as follows:

Copy code The code is as follows:

function Post($url, $post = null){
$context = array ();
if (is_array ( $post )) {
ksort ( $post );
$context ['http'] = array (
'timeout' => 60,
'method' => 'POST', 'content' => http_build_query( $post, '', '&' )
);

} return file_get_contents ( $url, false, stream_context_create ( $context ) );
}

$data = array (
'name' => 'test',
'email' => 'admin@admin.com',
'submit' => 'submit',
);
echo Post ( 'http://www.jb51.net', $data );

OK, the above function is perfect, solving both timeout control and Post value transfer.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/313508.htmlTechArticleTimeout processing of file_get_contents. Starting from PHP5, file_get_content already supports context (the manual says: 5.0.0 Added the context support. ), that is, starting from 5.0...
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