0

我有這個功能從外部URL獲取一些信息。 問題是,如果網站得到機器人沒有索引這個函數崩潰後崩潰foreach循環。file_get_html無法打開流:連接被拒絕

錯誤消息:

警告:的file_get_contents(http://webontwerp-arnhem.nl/contact):未能打開流:在/var/www/vhosts/free-sitemap-generator.com/httpdocs/includes/cra/simple_html_dom連接被拒絕.php on line 79

致命錯誤:未捕獲錯誤:調用成員函數在/var/www/vhosts/free-sitemap-generator.com/httpdocs/includes/cra/xml-functions中查找()布爾值.php:60 Stack trace:#0 /var/www/vhosts/free-sitemap-generator.com/httpdocs/crawler.php(44):crawl_site('http://webontwe ...')#1 {main}拋出/無功/網絡/虛擬主機/ free-sitemap-generator.com /的httpdocs /中cludes/CRA /上線XML-functions.php的60

功能:

function file_get_html($url, $use_include_path = false, $context=null, $offset = -1, $maxLen=-1, $lowercase = true, $forceTagsClosed=true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT) 
{ 

$dom = new simple_html_dom(null, $lowercase, $forceTagsClosed, $target_charset, $stripRN, $defaultBRText, $defaultSpanText); 

$contents = file_get_contents($url, $use_include_path, $context, $offset); 

if (empty($contents) || strlen($contents) > MAX_FILE_SIZE) 
{ 
    return false; 
} 

$dom->load($contents, $lowercase, $stripRN); 
return $dom; 
} 

調用與循環功能:

function crawl_site($u){ 
$urlList = array(); 
global $crawled_urls, $found_urls; 
$uen=urlencode($u); 
    if((array_key_exists($uen,$crawled_urls)==0 || $crawled_urls[$uen] < date("YmdHis",strtotime('-25 seconds', time())))){ 
    $html = file_get_html($u); 

     $crawled_urls[$uen]=date("YmdHis"); 
     foreach($html->find("a") as $li){ 
      $url=perfect_url($li->href,$u); 
      $enurl=urlencode($url); 
      $str = basename($url); 
      $dirn = dirname($url); 
       if($url!='' && substr($url,0,4)!="mail" && substr($url,0,3)!="tel" && substr($url,0,5)!="phone" && substr($url,0,5)!="skype" && substr($url,0,4)!="java" && array_key_exists($enurl,$found_urls)==0){ 
        $found_urls[$enurl]=1; 
        $pos = strpos($str[0],'#'); 
        $ext = strpos($url, $u); 
         if($ext !== false && $pos === false) { 
          echo "<li><div class='url-row'>$dirn/<span class='strong'>$str</span></div></li>"; 

          array_push($urlList, $url); 

         } 
       } 
     } 



} 
+0

如果錯誤是在$ html-> find(「a」)',在該行之前有一個「if」檢查:'if($ html){'....之後嘗試執行「foreach」 –

+0

是I已經嘗試,但例如:如果site.com/page_1沒有機器人meta標籤,但是site.com/page_2確實有機器人noindex。它不會循環site.com/page_1。那是我的問題。 –

回答

0

可以替代的file_get_contents的使用捲曲()

<?php 
    $url = 'http://webontwerp-arnhem.nl/contact';   
    $ch = curl_init();   
    curl_setopt($ch, CURLOPT_URL, $url);   
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);   
    $contents = curl_exec ($ch);   
    curl_close ($ch); 
+0

我試過了,它返回空。 –

+0

也許嘗試更改用戶代理 –

相關問題