Detailed PHP built-in Access resource timeout time_out file_get_contents read_file_php instance

Source: Internet
Author: User
Tags readfile
Questions
I loop through the file_get_contents crawl a bunch of URLs, but always stop at the 100th URL, prompting me: "Warning:file_get_contents (URL) [function.file-get-
Contents]: Failed to open stream:http request failed! http/1.0 Read timed out
In D:\website\extra.php on line 65 "
I have set_time_limit (0) at the beginning of the program; what's wrong with that?
Answer
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.
From the warning message, there is a server 500 error in the crawled Web page, it is possible that his program has timed out.
If you want to change the file_get_contents timeout, you can use 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);

So the ReadFile function timeout time is set to 10 seconds, if you are careful enough, you will also find that there are some other configuration in the array, the first dimension of HTTP is specified using the network protocol, two-dimensional method batch is the HTTP request method Get,post,head, etc. Timeout is the time-out. I think a lot of people use PHP's built-in file_get_contents function to download Web pages, because this function is simple enough to use. Many people also use it very simply, just pass a link it can send the GET request automatically, and download the content of the webpage. If a complex situation, such as using a POST request, using a proxy download, defining user-agent, and so on, then many people will think that this function can not do such things, will choose other ways, such as curl, to achieve. In fact, these things can be done file_get_contents,
is to set the context of the HTTP request through its third parameter
Supported settings and how to use the official note: http://www.php.net/manual/en/context.http.php
attached: Currently I know the PHP built-in functions that support the context parameter areFile_get_contents,file_put_contents,readfile,file,fopen,copy (This type of function is expected to support it, pending confirmation).
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.yifu.info ', $data);

OK, the above function is perfect, both solve the time-out control and solve the post transfer value. With the improved version of Hong Sing RC4 encryption and decryption algorithm, to do a very high security webservice is much simpler.
  • 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.