While including the simple HTML DOM library, I get the warnings:
Warning: file_get_contents() [function.file-get-contents]: php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\simple_html_dom.php on line 70
Warning: file_get_contents(http://www.google.com/) [function.file-get-contents]: failed to open stream: php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\simple_html_dom.php on line 70
The line 70 in simple_html_dom.php file(downloaded from http://sourceforge.net/projects/simplehtmldom/files/latest/download) is
$contents = file_get_contents($url, $use_include_path, $context, $offset);
Also 1 error:
Fatal error: Call to a member function find() on a non-object in C:\xampp\htdocs\domdoc2.php on line 15
where line 15 of the code(below) is
foreach($html->find('img') as $element)
The web page i was referring in my code below is google.com
Code follows:
<?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>';
?>
What am I doing wrong??
This is because your host was unable to resolve DNS, this happens when simplehtmldom uses file_get_contents instead of curl.
PHP Simple HTML DOM Parser is a great HTML parsing PHP class BUT it is slow since it uses
file_get_contents (which is disabled on almost all configurations) instead of cURL (4-5 times faster and with lots of options, almost every server has it).
Only file_get_contents is replaced so you can safely overwrite previous version and everything will work as before, only faster
Link to source code:
http://webarto.com/static/download/simple_html_dom.rar
However if you are completely new to HTML parsing in PHP Please Consider reading : How do you parse and process HTML/XML in PHP?