search
HomeBackend DevelopmentPHP TutorialDetailed explanation of PHP's built-in timeout for accessing resources time_out file_get_contents read_file_PHP tutorial

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

Jul 21, 2016 pm 03:09 PM
filegetoutphpreadtimebuilt-inIAsk a questiontimeofaccessDetailed explanationresourcetime out

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...
Statement
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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools