Solution to file_get_contents timeout in php

Source: Internet
Author: User
Tags time limit

Create a controllable resource handle and control the timeout of the file_get_contents method by controlling the resource handle timeout. This method is convenient and easy to use.

The code is as follows: Copy code


$ Context = stream_context_create (array (
'Http' => array (
'Timeout' => 3000 // timeout time, in seconds
     )
));
// Fetch the URL's contents
$ Contents = file_get_contents ('http: // www.111cn.net ', 0, $ context );


I. Added the timeout time limit.

Note: set_time_limit only sets the timeout time for your PHP program, rather than the timeout time 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: Copy code

$ Opts = array (
'Http' => array (
'Method' => "GET ",
'Timeout' => 60,
)
);
$ Context = stream_context_create ($ opts );
$ Html = file_get_contents ('http: // www.111cn.net ', false, $ context );

2. Try multiple times if there is a latency

Sometimes the failure is caused by factors such as the network and there is no solution, but you can modify the program and retry several times when the failure occurs. If the failure still fails, the program will be abandoned, because if file_get_contents () fails, FALSE will be returned, so you can write the code as follows:

 

The code is as follows: Copy code

$ Cnt = 0;

While ($ cnt <3 & ($ str = @ file_get_contents ('http... ') === 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: Copy code

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 '=> 'test @ gmail.com ',
'Submit '=> 'Submit ',
);

Echo Post ('http: // www.111cn.net ', $ data );

Related Article

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.