首页 / PHP / 如何修复PHP中的最大执行时间错误?
如何修复PHP中的最大执行时间错误?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何修复PHP中的最大执行时间错误?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2208字,纯文字阅读大概需要4分钟。
内容图文
![如何修复PHP中的最大执行时间错误?](/upload/InfoBanner/zyjiaocheng/688/8055668bab2c4c9dba0339ae5b2a0382.jpg)
我有一个PHP页面,每分钟都通过CRON作业运行.
我已经运行了一段时间,但突然开始出现以下错误:
Maximum execution time of 30 seconds exceeded in /home2/sharingi/public_html/scrape/functions.php on line 84
行号将随每个错误而变化,范围从行70到90年代.
这是0-95行中的代码
function crawl_page( $base_url, $target_url, $userAgent, $links)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); //follow up to 10 redirections - avoids loops
$html = curl_exec($ch);
if (!$html)
{
echo "<br />cURL error number:" .curl_errno($ch);
echo "<br />cURL error:" . curl_error($ch);
//exit;
}
//
// load scrapped data into the DOM
//
$dom = new DOMDocument();
@$dom->loadHTML($html);
//
// get only LINKS from the DOM with XPath
//
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");
//
// go through all the links and store to db or whatever
//
for ($i = 0; $i < $hrefs->length; $i++)
{
$href = $hrefs->item($i);
$url = $href->getAttribute('href');
//if the $url does not contain the web site base address: http://www.thesite.com/ then add it onto the front
$clean_link = clean_url( $base_url, $url, $target_url);
$clean_link = str_replace( "http://" , "" , $clean_link);
$clean_link = str_replace( "//" , "/" , $clean_link);
$links[] = $clean_link;
//removes empty array values
foreach($links as $key => $value)
{
if($value == "")
{
unset($links[$key]);
}
}
$links = array_values($links);
//removes javascript lines
foreach ($links as $key => $value)
{
if ( strpos( $value , "javascript:") !== FALSE )
{
unset($links[$key]);
}
}
$links = array_values($links);
// removes @ lines (email)
foreach ($links as $key => $value)
{
if ( strpos( $value , "@") !== FALSE || strpos( $value, 'mailto:') !== FALSE)
{
unset($links[$key]);
}
}
$links = array_values($links);
}
return $links;
}
是什么导致这些错误,如何防止它们发生?
解决方法:
您应该使用set_time_limit函数设置max_execution时间.如果您想要无限的时间(很可能是您的情况),请使用:
set_time_limit(0);
内容总结
以上是互联网集市为您收集整理的如何修复PHP中的最大执行时间错误?全部内容,希望文章能够帮你解决如何修复PHP中的最大执行时间错误?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。