2017-08-26 11 views
1

C:\ xampp \ htdocs \ college-project \ indexの非オブジェクトのプロパティを取得しようとしていますライン上の.php 1616行目のC: xampp htdocs college-project index.phpで非オブジェクトのプロパティを取得しようとしています

<?php 
    $start="http://localhost/college-project/test.html"; 

    $already_crawled=array(); 

    function get_details($url) 
    { 
     $options = array('http'=>array('method'=>'GET', 'headers'=>"User-Agent:howBot/0.1\n")); 

     $context=stream_context_create($options); 

     $doc = new DOMDocument(); 
     @$doc->loadHTML(@file_get_contents($url, false, $context)); 

     $title=$doc->getElementsByTagName("title"); 
     $title=$title->item(0)->nodeValue; 

     echo $title."\n"; 
    } 

    function follow_links($url) 
    { 
     global $already_crawled; 

     $options = array('http'=>array('method'=>'GET', 'headers'=>"User-Agent:howBot/0.1\n")); 

     $context=stream_context_create($options); 

     $doc = new DOMDocument(); 
     $doc->loadHTML(file_get_contents($url, false, $context)); 

     $linklist=$doc->getElementsByTagName("a"); 

     foreach($linklist as $link) 
     { 
      $l=$link->getAttribute("href"); 

      if(substr($l,0,1)=="/" && substr($l,0,2)!="//") 
      { 
       $l=parse_url($url)["scheme"]."://".parse_url($url)["host"].$l; 
      } 
      else if(substr($l,0,2)=="//") 
      { 
       $l = parse_url($url)["scheme"].":".$l; 
      } 
      else if(substr($l,0,2)=="./") 
      { 
       $l = parse_url($url)["scheme"]."://".parse_url($url)["host"].dirname(parse_url($url)["path"]).substr($l,1); 
      } 
      else if(substr($l,0,1)=="#") 
      { 
       $l = parse_url($url)["scheme"]."://".parse_url($url)["host"].parse_url($url)["path"].$l; 
      } 
      else if(substr($l,0,3)=="../") 
      { 
       $l = parse_url($url)["scheme"]."://".parse_url($url)["host"]."/".$l; 
      } 
      else if(substr($l,0,11)=="javascript:") 
      { 
       continue; 
      } 
      else if(substr($l,0,5)=="https" && (substr($l,0,4)=="http")) 
      { 
       $l=parse_url["scheme"].".//".parse_url($url)["host"]."/".$l; 
      } 
      if(!in_array($l,$already_crawled)) 
      { 
       $already_crawled[]=$l; 
       echo get_details($l); 
       //echo $l."\n"; 
      } 
     } 

    } 
    follow_links($start); 
    print_r($already_crawled); 
?> 
+1

をエコーし​​ようとする前に存在するかどうかを確認する –

+0

test.htmlというのGo to Google
A test page
ですtest.htmlという内容で、あなたの記事を更新してください
Another page
Another page
A link with an anchor
Some page
Another page
Even more
Javascript link

+0

だからあなたのtest.htmlというファイルには、タイトルを持っていない、あなたは、PHPのエラー –

答えて

0

あなたは

function get_details($url) 
{ 
    $options = array('http'=>array('method'=>'GET', 'headers'=>"User-Agent:howBot/0.1\n")); 

    $context=stream_context_create($options); 

    $doc = new DOMDocument(); 
    @$doc->loadHTML(@file_get_contents($url, false, $context)); 

    $title=$doc->getElementsByTagName("title"); 
    if($title->length AND is_object($title)) 
    { 
     $title=$title->item(0)->nodeValue; 
    }else{ 
     $title=''; 
    } 


    echo $title."\n"; 
} 
+0

アイテム(0)は何も返されないので、これは失敗する可能性があると思います。 $ titleはアイテムのないnodeListになります – Andreas

+0

@Andreas:タイトル長のチェックで更新されました。 –

+0

が動作していないことを指摘してくれてありがとうございます –

関連する問題