Detailed explanation of PHP's built-in timeout for accessing resources time_out file_get_contents read_file_PHP tutorial

WBOY
Release: 2016-07-21 15:09:49
Original
901 people have browsed it

Question
I use file_get_contents in a loop to crawl a bunch of URLs, but it always stops before the 100th URL, 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:websiteextra.php on line 65”
I am in the program There is already set_time_limit(0); at the beginning, so what could be the reason for the above error?
Answer
set_time_limit only sets the timeout of your PHP program, not the timeout of the file_get_contents function reading the URL.
Judging from the warning message, a server 500 error occurred in the crawled web page. It may be that his program has timed out.
If you want to change the timeout of file_get_contents, you can use the timeout parameter of resource $context:

Copy the code 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 timeout of the readfile function is set to 10 seconds. If you are careful enough, you will also find some other configurations in the array. The http in the first dimension is the specified network protocol. , the method in 2D refers to http request methods such as get, post, head, etc., and timeout is the timeout. I think many people will use PHP's built-in file_get_contents function to download web pages, because this function is simple enough to use. Many people use it very simply. Just pass a link and it can automatically send a get request and download the web content. If there are more complex situations, such as using POST requests, using proxy downloads, defining User-Agent, etc., then many people will think that this function cannot do such things, and will choose other methods, such as curl, to achieve it. In fact, file_get_contents can also do these things.
sets the context of the http request through its third parameter.
For supported settings and usage, please see the official instructions: http://www.php.net/manual/en/context.http.php
Attachment: Currently I know of built-in PHP that supports context parameters The functions include file_get_contents, file_put_contents, readfile, file, fopen, copy (it is estimated that these functions are supported, to be confirmed).
Copy code 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, solving both timeout control and Post value transfer. Coupled with Kangsheng's improved version of RC4 encryption and decryption algorithm, it is much easier to build a highly secure webservice.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327194.htmlTechArticleQuestion I use file_get_contents to crawl a bunch of URLs in a loop, but it always ends up before the 100th URL. Stopped and prompted me: "Warning: file_get_contents(URL) [function.file-get- contents...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!