First of all, solve the file_get_contents timeout problem, after the timeout return error, like JS in the settimeout as an attempt, error more than 3 times or 5 times after the confirmation as unable to connect the server and completely give up.
Here's a quick way to introduce two solutions:
One, increase time limit of timeout
Note: Set_time_limit only sets the timeout time 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:
PHP code
$opts = Array (
' HTTP ' =>array (
' Method ' => ' get ',
' Timeout ' =>60,
)
);
$context = Stream_context_create ($opts);
$html =file_get_contents (' http://www.111cn.net ', false, $context);
Fpassthru ($FP);
Second, many attempts
PHP code
$cnt = 0;
while ($cnt < 3 && ($str = @file_get_contents (' http ... ')) {===false) {
$cnt + +;
}
The above method of dealing with timeout is OK. Next, I'll show you how to implement post with File_get_contents, as follows:
PHP 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);
Pay attention to the set_time_out of the file, or the entire file will be timed out.