2012-06-22 146 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时。 –