In-depth explanation of the php function file_get_contents timeout processing method. 1. add the timeout time limit. Note that set_time_limit only sets the timeout time for your PHP program, rather than the timeout time for file_get_contents function to read the URL. Real repairs
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. 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' => 60,
));
$ Context = stream_context_create ($ opts); $ html = file_get_contents ('http: // www.example.com ', 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:
$ Cnt = 0;
While ($ cnt <3 & ($ str = @ file_get_contents ('http... ') === FALSE) $ cnt ++;
The above method has timed out.
Someone found that 'method' => "GET" and GET can also be set to post. the function is as follows:
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 '=> 'test @ gmail.com ',
'Submit '=> 'Submit ',
);
Echo Post ('http: // www.example.com ', $ data );
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. Real repairs...