


Detailed explanation of PHP's built-in timeout for accessing resources time_out file_get_contents read_file_PHP tutorial
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:
$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).
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.

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

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.

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

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

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

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.

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

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

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
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools
