2012-04-12 73 views
0

爲什麼我收到以下錯誤PHP - 錯誤與DOM文檔

Call to a member function getElementsByTagName() on a non-object 

錯誤發生在下面一行:

$title = $htm->getElementsByTagName('title'); 

當我運行下面的代碼:

$dom = new DOMDocument(); 

    foreach ($all as $blog) { 

    sleep(1); 

    $htm = $dom->loadHTML(fetch_url('http://' . rtrim(preg_replace('/^http:\/\//i', '', $blog['blogurl']), '/'))); 

    if ($htm) { 

     //check TITLE 

     $title = $htm->getElementsByTagName('title'); 

     $title = $title->item(0)->nodeValue; 

     if (preg_match('/private/i', $title)) { 

     private_blog($blog['id']); 

     $title = null; 
     unset($title); 
     gc_collect_cycles(); 
     continue; 
     } 
    } 
} 

回答

0

剛在該行中替換$htm$dom

$title = $dom->getElementsByTagName('title'); 
     ^^^^ 

進一步提高在上面的其他線重命名$htm變量:

$loadResult = $dom->loadHTML(fetch_url('http://' . rtrim(preg_replace('/^http:\/\//i', '', $blog['blogurl']), '/'))); 
^^^^^^^^^^^ 
if ($loadResult) { 
    ^^^^^^^^^^^