PHP file_get_contents Set Timeout processing method _php Tips

Source: Internet
Author: User
Tags curl

Timeout processing for file_get_contents

In other words, from PHP5 onwards, File_get_content has already supported the context (the manual reads: 5.0.0 Added the context support. , that is, starting at 5.0, file_get_contents can actually post data.

Today's said this is to say timeout, it is true in the cross-server submission, the inevitable encounter timeout situation, this time how to do? Set_time_limit is not used, only in the context of the timeout time to control. On the contrary, we are not to restrain, but to manage. For example, after the timeout returned to an error, an attempt, like the settimeout in JS, the function of reprocessing. After 3 or 5 errors, we really think we can't connect to the server and give up completely. This, is a good way, should be worth recommending to use. In fact. Not all file_get_contents, as long as the support context should be added to avoid time wasting. The functions that can be supported are roughly: Fsocketopen (the last parameter of the function). It seems to be more recommended when reading the stream, using the Stream_time_out function for control), fopen (also from the PHP5 to join the context support), file (PHP5 join support), curl (curl has its own variable Curlopt_ TIMEOUT) and so on.

When using the File_get_contents function, there is often a time-out, where you have to check the error prompts to see what kind of error, the more common is the read timeout, this situation can be used to try to avoid or solve. Here is a brief description of two:

One, increase time limit of timeout

Note here: Set_time_limit only sets the timeout for your PHP program, not the timeout for the file_get_contents function to read the URL.

I initially thought Set_time_limit could also affect File_get_contents, which was later tested to be ineffective. The real modification of the file_get_contents delay can be resource $context timeout parameters:

Copy Code code 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;

If 二、一次 has a delay, try it a few more times.

Sometimes failure is due to the network and other factors, there is no solution, but can modify the program, failed to retry several times, still fail to give up, because file_get_contents () if the failure will return FALSE, so you can write the following code:

Copy Code code 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 of dealing with timeout is OK. So what about post? Careful, someone found the ' method ' => ' get ', yes! Is it possible to set up a post? Baidu looked for the next relevant information, really can! And someone wrote the version of the Post pass function, as follows:

Copy Code code 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, both to solve the timeout control and solve the post pass value.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.