I. Increase the time limit for timeouts
Note here: Set_time_limit only sets the time-out for your PHP program, not the time-out for the file_get_contents function to read the URL. The real modification file_get_contents delay can be used with the timeout parameter of the resource $context:
Copy CodeThe 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);
if the 二、一次 is delayed, try a few more times.
Sometimes the failure is caused by 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:
$cnt = 0;
while ($cnt < 3 && ($str = @file_get_contents (' http ... ')) ===false) $cnt + +;
The above method against timeout is OK.
Someone found the ' method ' and ' get ', get can also be set to post, function as follows
Copy CodeThe 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);
http://www.bkjia.com/PHPjc/327191.html www.bkjia.com true http://www.bkjia.com/PHPjc/327191.html techarticle I. Increasing the time limit for timeouts Here's a note: Set_time_limit just sets the time-out for your PHP program, not the time-out of the file_get_contents function to read the URL. The real fix ...