PHP file_get_contents function Timeout several workarounds _php tutorial

Source: Internet
Author: User
Here is a brief introduction to two types:
One, 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.
At first I thought set_time_limit could also affect File_get_contents, which was later tested to be ineffective. 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);
Fpassthru ($FP);

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:
Copy CodeThe code is as follows:
$cnt = 0;
while ($cnt < 3 && ($str = @file_get_contents (' http ... ')) ===false) $cnt + +;

http://www.bkjia.com/PHPjc/320466.html www.bkjia.com true http://www.bkjia.com/PHPjc/320466.html techarticle Here is a brief introduction of two kinds: first, increase the time limit of the timeout here to note: Set_time_limit just set your PHP program timeout time, not file_get_contents function read ...

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