This article provides a detailed analysis of the timeout time_outfile_get_contentsread_file for PHP built-in resource access. For more information, see
Question I cyclically crawl a bunch of URLs with file_get_contents, but it will always stop when there are no more than 100th URLs, prompting me: "Warning: file_get_contents (url) [function. file-get-
Contents]: failed to open stream: HTTP request failed! HTTP/1.0 500 Read timed out
In D: \ website \ extra. php on line 65"
I already have set_time_limit (0) at the beginning of the program. what is the above error?
Answer Set_time_limit only sets the timeout value for your PHP program, instead of the timeout value for the file_get_contents function to read the URL.
According to the warning information, a server error 500 occurs on the crawled webpage, possibly because the program times out.
To change the file_get_contents timeout, you can use the timeout parameter of resource $ context:
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 );
Fpassthru ($ fp );
In this way, the readfile function time-out time is set to 10 seconds. if you are careful enough, you will also find some other configurations in the array. in the first dimension, http is the specified network protocol, in the two-dimensional method batch, the http request methods include get, post, head, and so on. timeout is the timeout time. I think many people will use the built-in file_get_contents function of php to download the webpage, because this function is easy to use. Many people use it easily. they can send a get request automatically and download the webpage content by passing a link. If it is complicated, such as using POST requests, using a proxy for download, and defining a User-Agent, many people will think that this function cannot do such a thing and choose other methods, such as curl. In fact, file_get_contents can also do these things,
The third parameter sets the context of the http request..
Supported settings and usage see official instructions: http://www.php.net/manual/en/context.http.php
Appendix: currently, I know the built-in php functions that support the context parameter.File_get_contents, file_put_contents, readfile, file, fopen, and copy (it is estimated that all functions of this type are supported, to be confirmed ).
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.yifu.info ', $ data );
OK, the above function is perfect, both timeout control and Post value passing are solved. In combination with Kangsheng's improved version of RC4 encryption and decryption algorithm, it is much easier to create a secure webservice.
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.