Reputation: 3
Hey guys I'm Have this code:
<?php
include './simple_html_dom.php';
//this link exists
$teste = new simple_html_dom("http://www.btolinux.com.br/");
echo $teste->original_size."<br>";
if($teste->original_size !== 0){
$teste->find("html");
}
//At this time I'm forcing get a 404 error link.
$teste = new simple_html_dom("http://www.btolinux.com.br/error/");
echo $teste->original_size."<br>";
if($teste->original_size !== 0){
$teste->find("html");
}
//Now I'm get the correct link again. Why the error persists?
$teste = new simple_html_dom("http://www.btolinux.com.br/");
echo $teste->original_size."<br>";
if($teste->original_size !== 0){
$teste->find("html");
}
?>
So, in my code, when I found a 404 error link, in the next objects created with the same class, the error persists. How can I solve this problem?
To try this code get the Simple_dom_php in http://sourceforge.net/projects/simplehtmldom/files/
Upvotes: 0
Views: 6031
Reputation: 4185
Try this code:
<?php
require_once("./simple_html_dom.php"); # simplehtmldom.sourceforge.net
$url="http://www.btolinux.com.br/";
$url404="http://www.btolinux.com.br/error/";
$teste = @file_get_html($url);
if ($teste && $teste->original_size) {
echo $teste->original_size."<br>\n";
$html = $teste->find("html");
}
$teste = @file_get_html($url404);
if ($teste && $teste->original_size) {
echo $teste->original_size."<br>\n";
$html = $teste->find("html");
}
$teste = @file_get_html($url);
if ($teste && $teste->original_size) {
echo $teste->original_size."<br>\n";
$html = $teste->find("html");
}
?>
My output was:
61206<br>
61206<br>
Upvotes: 1
Reputation: 2113
Try the @ to avoid warnings, like this:
<?php
//CORRECT
$url = "http://www.btolinux.com.br/";
$html = @file_get_contents($url);
if ($html!='') {
$teste = new simple_html_dom($url);
echo 'SIZE: '.$teste->original_size."<br>";
}
//ERROR
$url = "http://www.btolinux.com.br/error/";
$html = @file_get_contents($url);
if ($html!='') {
$teste = new simple_html_dom($url);
echo 'SIZE: '.$teste->original_size."<br>";
}
?>
Upvotes: 2
Reputation: 121669
SUGGESTION:
$teste = new simple_html_dom();
$teste->load_file("http://www.btolinux.com.br/");
...
... OR ...
$teste = file_get_html("http://www.btolinux.com.br/");
Upvotes: 0
Reputation: 2113
Have you tried using file_get_contents() first? For example if you use the code:
<?php
print_r(file_get_contents("http://www.btolinux.com.br/error/"));
?>
You'll see that there's an error, then you can try-catch it.
Upvotes: 0