0

外部URLから情報を取得するこの機能があります。 問題は、Webサイトにロボットのインデックスがない場合、この関数がクラッシュし、その後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にあるbooleanのメンバー関数find()を呼び出します。 .php:60スタックトレース:#0 /var/www/vhosts/free-sitemap-generator.com/httpdocs/crawler.php(44):crawl_site( 'http://webontwe ...')#1 {main} var/www/vhosts/free-sitemap-generator.com/httpdocs/inライン上の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

はいたとえば、try/tryを実行します。たとえば、site.com/page_1にロボットのメタタグがない場合、site.com/page_2にnoindexというロボットがある場合ループはsite.com/page_1しません。それは私の問題です。 –

答えて

0

あなたは、のfile_get_contentsの代わりにCURLを使用することができます()

<?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

tryd、空を返します。 –

+0

おそらくUser-Agentを変更しようとします。 –

関連する問題