2

在包含简单的 HTML DOM 库时,我收到了警告:

警告:file_get_contents() [function.file-get-contents]: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 失败:不知道这样的主机。在第 70 行的 C:\xampp\htdocs\simple_html_dom.php

simple_html_dom.php 文件(从http://sourceforge.net/projects/simplehtmldom/files/latest/download下载)中的第 70 行是

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

还有1个错误:

致命错误:在第 15 行的 C:\xampp\htdocs\domdoc2.php 中的非对象上调用成员函数 find()

其中代码(下)的第 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>';
?>

我究竟做错了什么??

4

2 回答 2

2

这是因为您的主机无法解析 DNS,当 simplehtmldom 使用 file_get_contents 而不是 curl 时会发生这种情况。PHP Simple HTML DOM Parser 是一个很棒的 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/

但是,如果您对 PHP 中的 HTML 解析完全陌生,请考虑阅读:您如何在 PHP 中解析和处理 HTML/XML?

于 2012-06-22T07:27:28.793 回答
1

这与simple_html_dom. 您的服务器无法访问 Internet,并且无法解析google.com。检查 DNS 设置,也许还有防火墙。

于 2012-06-22T07:22:32.500 回答