2012-06-22 90 views
2

雖然包括簡單的HTML DOM庫,我得到的警告:簡單的HTML DOM庫

警告:file_get_contents()函數[function.file-GET-內容]:php_network_getaddresses:的getaddrinfo失敗:沒有這樣的主機是已知的。在線70上的C:\ xampp \ htdocs \ simple_html_dom.php

警告:file_get_contents(http://www.google.com/)[function.file-get-contents]:無法打開流:php_network_getaddresses: getaddrinfo失敗:沒有這樣的主機是已知的。在C:\ XAMPP \ htdocs中\ simple_html_dom.php上線70

在simple_html_dom.php文件(從http://sourceforge.net/projects/simplehtmldom/files/latest/download下載)的線70是

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

另外1錯誤:

致命錯誤:調用一個成員函數查找()在C語言的非對象:\ XAMPP \ htdocs中\ domdoc2.php第15行

在碼(下圖)的線15是

foreach($html->find('img') as $element) 

的網頁我在我下面的代碼指是google.com 代碼如下:

 <?php 

include('simple_html_dom.php'); 
$html = new simple_html_dom(); 
$html = file_get_html('http://www.google.com/'); 
// Find all images 
foreach($html->find('img') as $element) 
     echo $element->src . '<br>'; 

// Find all links 
foreach($html->find('a') as $element) 
     echo $element->href . '<br>'; 
?> 

我在做什麼錯?

+0

這不是一個圖書館的問題,PHP只是無法解析主機到一個IP地址 –

+0

該文件執行那裏? –

+0

@Baszz你指的是哪個文件? DOM文件在那裏,它也包含在內 – saur

回答

1

這是因爲您的主機無法解析DNS,這發生在simplehtmldom使用file_get_contents而不是curl時。 PHP簡單的HTML DOM解析器是一個偉大的HTML解析PHP類,但它很慢,因爲它使用 file_get_contents(它在幾乎所有配置上都被禁用)而不是cURL(速度提高4-5倍,並且有很多選項,幾乎每個服務器都有它)。

只的file_get_contents被替換,所以你可以安全地覆蓋之前的版本,一切都會像以前一樣,只是速度更快

鏈接到源代碼: http://webarto.com/static/download/simple_html_dom.rar

//output should be 

/intl/en_ALL/images/srpr/logo1w.png 
http://www.google.com/webhp?hl=en&tab=ww 
http://www.google.com/imghp?hl=en&tab=wi 
http://maps.google.com/maps?hl=en&tab=wl 
https://play.google.com/?hl=en&tab=w8 
http://www.youtube.com/?tab=w1 
http://news.google.com/nwshp?hl=en&tab=wn 
https://mail.google.com/mail/?tab=wm 
https://docs.google.com/?tab=wo 
http://www.google.com/intl/en/options/ 
https://www.google.com/calendar?tab=wc 
http://translate.google.com/?hl=en&tab=wT 
http://www.google.com/mobile/?tab=wD 
http://books.google.com/bkshp?hl=en&tab=wp 
https://www.google.com/offers/home?utm_source=xsell&utm_medium=products&utm_campaign=sandbar&tab=wG#!details 
https://wallet.google.com/manage/?tab=wa 
http://www.google.com/shopping?hl=en&tab=wf 
http://www.blogger.com/?tab=wj 
http://www.google.com/reader/?hl=en&tab=wy 
http://www.google.com/finance?tab=we 
http://picasaweb.google.com/home?hl=en&tab=wq 
http://video.google.com/?hl=en&tab=wv 
http://www.google.com/intl/en/options/ 
https://accounts.google.com/ServiceLogin?hl=en&continue=http://www.google.com/ 
http://www.google.com/preferences?hl=en 
/preferences?hl=en 
/url?sa=p&pref=ig&pval=3&q=http://www.google.com/ig%3Fhl%3Den%26source%3Diglk&usg=AFQjCNFA18XPfgb7dKnXfKz7x7g1GDH1tg 
http://www.google.com/history/optout?hl=en 
/advanced_search?hl=en 
/language_tools?hl=en 
/intl/en/ads/ 
/services/ 
https://plus.google.com/116899029375914044550 
/intl/en/about.html 
/intl/en/policies/ 

但是,如果你是完全新的HTML在PHP解析請考慮閱讀:How do you parse and process HTML/XML in PHP?

+0

謝謝你..這可能聽起來太業餘了,但請你告訴應該輸出什麼..我得到一個空白屏幕.. – saur

+0

它應該打印出所有的鏈接(a-> href)和圖像(img - > src),如你在腳本中所要求的那樣。我嘗試了完全相同的腳本,並且用輸出更新了ans。 –

+0

謝謝你! – saur

1

這和simple_html_dom沒有任何關係。您的服務器沒有互聯網訪問權限,但無法解析google.com。檢查DNS設置,也許是防火牆。

+0

服務器具有Internet訪問權限,仍然收到此錯誤消息。任何線索? – saur

+0

這是因爲您的主機無法解析DNS,這發生在simplehtmldom使用file_get_contents而不是curl時。 –