如何使用 PHP 获得无限的最大执行时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7036767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get infinite maximum execution time with PHP?
提问by Paul Mason
I have a site with 2000 pages and I want to iterate through each page to generate a sitemap, using the file_get_html()
function and regular expressions.
我有一个包含 2000 个页面的站点,我想使用file_get_html()
函数和正则表达式遍历每个页面以生成站点地图。
Obviously this can't be completed in one server-side execution as it will run out of time due to maximum execution time. I guess it needs to perform smaller actions, save the progress to the database and then queue the next task. Any suggestions?
显然,这不能在一个服务器端执行中完成,因为它会因最长执行时间而用完。我猜它需要执行较小的操作,将进度保存到数据库,然后将下一个任务排队。有什么建议?
回答by Wesley van Opdorp
When you run it command line there will be no maximum execution time.
当您在命令行中运行它时,将没有最大执行时间。
You can also use set_time_limit(0);
for this if your provider allows manipulation.
set_time_limit(0);
如果您的提供者允许操作,您也可以用于此目的。
I can't tell if your ip-address will get banned - as this depends on the security of the server you send your requests to.
我不知道您的 IP 地址是否会被禁止 - 因为这取决于您向其发送请求的服务器的安全性。
Other solution
其他解决方案
You can fetch one (or a few) page(s), and search for new URLs throughout the source code. You can then queue these in a database. Then on the next run, you process the queue.
您可以获取一个(或几个)页面,并在整个源代码中搜索新的 URL。然后,您可以将它们放入数据库中。然后在下一次运行中,您处理队列。
回答by Shoan
回答by Madara's Ghost
Use set_time_limit(0)
. See the PHP Manualfor more detailed explanation.
使用set_time_limit(0)
. 有关更详细的解释,请参阅PHP 手册。
seconds
The maximum execution time, in seconds. If set to zero, no time limit is imposed.
秒
最大执行时间,以秒为单位。如果设置为零,则没有时间限制。
EDIT: As for your second question, it's not likely, however, you should check your hosting services Terms of Use to see if it's allowed.
编辑:至于您的第二个问题,不太可能,但是,您应该检查您的托管服务使用条款,看看它是否被允许。
回答by Kirrus
Set max_execution_time to 0 in your php.ini. It will affect every script you run on the server, but if you're looking for a server-level fix, this will do it.
在 php.ini 中将 max_execution_time 设置为 0。它会影响您在服务器上运行的每个脚本,但如果您正在寻找服务器级别的修复程序,则可以这样做。
http://php.net/manual/en/info.configuration.php#ini.max-execution-time
http://php.net/manual/en/info.configuration.php#ini.max-execution-time
max_execution_time = 0
回答by AliHossinzadeh
the best way for you is use remot api . for example you can use import.io and get param from each page with json format . this is a way to get light page on each call for file_get_content or flie_get_html
对您来说最好的方法是使用 remot api 。例如,您可以使用 import.io 并从每个页面以 json 格式获取参数。这是在每次调用 file_get_content 或 flie_get_html 时获取轻页面的方法
but for this issu curl is beter than file_get_html
但是对于这个问题,curl 比 file_get_html 好