PHP脚本在执行期间似乎超时

时间:2015-01-02 21:18:00

标签: php timeout

我编写了一个脚本来检查页面上的链接是否断开。它对于少于一百个左右链接的页面非常有用,但是一旦脚本运行超过60秒,我就会收到500错误。

我尝试修改php ini设置(最大执行时间目前设置为300)。我还使用了set_time_limit和ini_set()。我读过无数帖子,但仍未找到解决方案。我将在下面发布代码,希望有人可以告诉我出了什么问题。

<?php
//INITIALIZE VARIABLES
$pageToCheck    = $_GET['link'];

//INITIALIZE DOMDOCUMENT
$domDoc = new DOMDocument;
$domDoc->preserveWhiteSpace = false;

//IF THE PAGE BEING CHECKED LOADS
if(@$domDoc->loadHTMLFile($pageToCheck)) { //note that errors are suppressed so DOMDocument doesn't complain about XHTML
     //LOOP THROUGH ANCHOR TAGS IN THE MAIN CONTENT AREA
     $pageLinks = $domDoc->getElementsByTagName('a');
     foreach($pageLinks as $currLink) {
          //LOOP THROUGH ATTRIBUTES FOR CURRENT LINK
          foreach($currLink->attributes as $attributeName=>$attributeValue) {
               //IF CURRENT ATTRIBUTE CONTAINS THE WEBSITE ADDRESS
               if($attributeName == 'href' && $attributeValue->value != "" && $attributeValue->value != "#") {

                         if (preg_match("/webapp/", $attributeValue->value)) { 
                              $lCheck = $pageToCheck . $attributeValue->value;
                         }
                         else
                         {
                              $lCheck = $attributeValue->value;
                         }

                         $check_url_status = check_url($lCheck);

                         if ($check_url_status != "200" && $check_url_status != "301" && $check_url_status != "302")
                         {
                              echo "<span style=\"color: #FF0000; font-weight: bold;\">Broken Link: </span><span style=\"color: #0000FF; font-weight: bold;\">Status: $check_url_status</span> $attributeValue->value <br>";
                         }
                         else
                         {
                              echo "<span style=\"color: #00FF00; font-weight: bold;\">Working Link: </span><span style=\"color: #0000FF; font-weight: bold;\">Status: $check_url_status</span> $attributeValue->value <br>";
                         }

               }
          }
     }

}

function check_url($url) {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($ch);
    $headers = curl_getinfo($ch);
    curl_close($ch);

    return $headers['http_code'];
}

?>

0 个答案:

没有答案