2015-04-30 33 views
3

我使用simple_html_dom網絡報廢應用程序的工作。我需要提取網頁中的所有圖像。以下是可能:PHP致命錯誤:無法使用類型simple_html_dom的對象數組

  1. <img>標籤圖像
  2. 如果在同一頁面中<style>標籤的CSS。
  3. 如果與<div>或與其他標籤內嵌樣式的圖像。

我可以通過使用下面的代碼颳去所有圖像。

function download_images($html, $page_url , $local_url){ 

    foreach($html->find('img') as $element) { 
     $img_url = $element->src; 
     $img_url = rel2abs($img_url, $page_url); 
     $parts = parse_url($img_url); 
     $img_path= $parts['path']; 
     $url_to_be_change = $GLOBALS['website_server_root'].$img_path; 
     download_file($img_url, $GLOBALS['website_local_root'].$img_path); 
     $element->src=$url_to_be_change;    
    } 

    $css_inline = $html->find("style"); 

    $matches = array(); 
    preg_match_all("/url\((.*?)\)/", $css_inline, $matches, PREG_SET_ORDER); 
    foreach ($matches as $match) { 
     $img_url = trim($match[1], "\"'"); 
     $img_url = rel2abs($img_url, $page_url); 
     $parts = parse_url($img_url); 
     $img_path= $parts['path']; 
     $url_to_be_change = $GLOBALS['website_server_root'].$img_path ; 
     download_file($img_url , $GLOBALS['website_local_root'].$img_path); 
     $html = str_replace($img_url , $url_to_be_change , $html); 
    } 

    return $html; 
} 

$html = download_images($html , $page_url , $dir); // working fine 
$html = str_get_html ($html); 
$html->save($dir. "/" . $ff);  

請注意,我在下載圖片後也修改了HTML。

下載工作正常。但是,當我試圖保存HTML,那麼它給了以下錯誤:

PHP Fatal error: Cannot use object of type simple_html_dom as array

重要:其工作完全正常,如果我不使用str_replace和第二循環。

Fatal error: Cannot use object of type simple_html_dom as array in /var/www/html/app/framework/cache/includes/simple_html_dom.php on line 1167

+0

的$ HTML作爲最後一個參數在你的str_replace調用中是一個對象,而不是一個數組。 str_replace顯然不喜歡那樣。您需要找出另一種將數據表示爲數組的方法,或者以某種方式重新處理它。 –

+0

obligatory http://stackoverflow.com/a/1732454/3044080 – nomistic

回答

0

由於錯誤消息指出,您正在處理的對象應該有一個數組。 你可以嘗試tpyecasting你的對象:

$array = (array) $yourObject; 

這應該解決這個問題。

1

猜測№1

我看到一個可能的錯誤在這裏:

$html = str_get_html($html); 

看起來你傳遞一個對象的功能str_get_html(),而它接受一個字符串作爲參數。讓我們解決這個問題是這樣的:

$html = str_get_html($html->plaintext); 

我們只能猜測是什麼$ HTML變量的內容,即涉及到這段代碼。

猜測№2

或者,也許我們只需要在功能download_images使你的代碼正確的在這兩種情況下使用另一個變量:

function download_images($html, $page_url , $local_url){ 

    foreach($html->find('img') as $element) { 
     $img_url = $element->src; 
     $img_url = rel2abs($img_url, $page_url); 
     $parts = parse_url($img_url); 
     $img_path= $parts['path']; 
     $url_to_be_change = $GLOBALS['website_server_root'].$img_path ; 
     download_file($img_url , $GLOBALS['website_local_root'].$img_path); 
     $element->src=$url_to_be_change;    
    } 

    $css_inline = $html->find("style"); 

    $result_html = ""; 
    $matches = array(); 
    preg_match_all("/url\((.*?)\)/", $css_inline, $matches, PREG_SET_ORDER); 
    foreach ($matches as $match) { 
     $img_url = trim($match[1], "\"'"); 
     $img_url = rel2abs($img_url, $page_url); 
     $parts = parse_url($img_url); 
     $img_path= $parts['path']; 
     $url_to_be_change = $GLOBALS['website_server_root'].$img_path ; 
     download_file($img_url , $GLOBALS['website_local_root'].$img_path); 
     $result_html = str_replace($img_url , $url_to_be_change , $html); 
    } 

    return $result_html; 
} 

$html = download_images($html , $page_url , $dir); // working fine 
$html = str_get_html ($html); 
$html->save($dir. "/" . $ff); 

說明:如果沒有匹配(陣列$ matches是空的)我們永遠不會進入第二個循環,這就是爲什麼變量$ html仍然具有與函數開始時相同的值。當你試圖在需要兩個不同變量的代碼中使用相同的變量時,這是常見的錯誤。

+0

第1167行:if($ this-> size> 0)$ this-> char = $ this-> doc [0]; – user2674341

+0

更新了我的答案。增加了一個解決方案(參見猜測№2部分)。請告訴我在所有情況下這兩項工作中的哪一項。 –

+0

現在,它顯示這個錯誤,我不能看到你的第二個解決方案。 PHP致命錯誤:調用一個非對象的成員函數save()在 – user2674341

0

我有這個錯誤,我解決了它通過使用(在我的情況)返回$ html-> save();在函數結束。 我無法解釋爲什麼具有不同變量名稱的兩個實例,以及在不同函數中作用的實例發生此錯誤。我想這是「簡單的HTML DOM」類的工作原理。

所以僅僅是明確的,嘗試:$ HTML的「保存(),你以後

做任何事情之前,我希望這個信息可以幫助別人:)

相關問題