PHPfile_get_contents _ PHP Tutorial

Source: Internet
Author: User
PHPfile_get_contents sets the timeout processing method. File_get_contents timeout processing: starting from PHP5, file_get_content already supports context (the manual says: 5.0.0Addedthecontextsupport.), that is, file_get_contents timeout processing starts from 5.0.

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

This article is about timeout. it is true that when cross-server submission is performed, timeout may inevitably occur. what should I do at this time? Set_time_limit is useless and can only be controlled using the timeout time in context. On the contrary, we do not want to stop, but to manage. For example, if an error is returned after a timeout occurs, reprocess the function like settimeout in js. After three or five errors, we do think that we cannot connect to the server and give up completely. This is a good method and should be recommended. Actually. Not all are file_get_contents. if context is supported, it should be added to avoid time-out waste. The supported functions include fsocketopen (the last parameter of the function. It seems that we recommend that you use the stream_time_out function to control stream reading.) fopen (also supports context from PHP5), file (supports PHP5 ), curl (curl has its own variable CURLOPT_TIMEOUT.

When using the file_get_contents function, timeout often occurs. here, you need to check the error message to see which error is returned. it is more common to see when the read is too large, in this case, you can try to avoid or solve the problem. Here we will briefly introduce two types:

I. added the timeout time limit.

Note: set_time_limit only sets the timeout value for your PHP program, instead of the timeout value for the file_get_contents function to read the URL.

At first, I thought that set_time_limit could also affect file_get_contents. after testing, it was invalid. You can use the timeout parameter of resource $ context to modify the file_get_contents latency:

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. try multiple times if there is a latency

Sometimes the failure is caused by factors such as the network. there is no solution, but you can modify the program. if the failure occurs several times, you can give up. because file_get_contents () returns FALSE if the failure occurs, so you can write the code as follows:

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 has timed out. What about Post? Be careful when someone finds 'method' => "GET", right! Can it be set to post? Baidu has found the relevant information. you can do it! In addition, someone wrote the post value passing function in the shanzhai version, as shown below:

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, both timeout control and Post value passing are solved.

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...

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.