Reputation: 97
When I try to scrape data from some website using say $url
using simple html DOM . After few days links get outdated and i get an saying http request failed.So instead of displaying that error.I want to display something else that I want.
For example here is the code :
<?
$html=file_get_html($url);
$title=$html->find("stuff that I want to extract",0)->plaintext;
if($title)
{
echo $title;
}
else{
echo 'problem';
}
?>
In the above example it displays problem only if that data is not found but I need to display problem if I get http request errors too.
Upvotes: 3
Views: 1636
Reputation: 2943
error_reporting level is a valid solution. This can throw exceptions and you can use that to handle your errors. There are many reasons why file_get_html might generate warnings, and PHP's manual itself recommends lowering error_reporting.
Or might be this will help you:
$html = file_get_html($url) or die('this is not a valid url');
file_get_html returns false if any issue while getting data.
$html = file_get_html($url);
if ($html) {
// code
} else {
// error
}
Use Try-Catch
try {
$html = file_get_html(trim($url));
} catch (Exception $e) {
// handle error here
}
Suggestion: Use CURL to get the URL and handle the error response.
Upvotes: 2