Answer the question
In order to leave comments, you need to log in
How to catch 404 error when parsing?
Hello!
There is a simple and stupid php code with simple_html_dom.php. Pars just to them. There are many product pages, but for example, there are 100 and 102, but no 101. How do I catch a 404 error?
$e = 'HTTP_Exception_404';
try {
$data = file_get_html('link');
} catch (Exception $e) {
echo $e;
}
Answer the question
In order to leave comments, you need to log in
Why not parse the 404 page with the same simple_html_dom?
I.e
try {
$data = file_get_html('ссылка');
// например, на странице с ошибкой текст ошибки '<div class="error">... Ошибка 404, товар не найден ...</div>'
if (false !== strpos($data->find('div.error', 0)->plaintext, '404')) {
throw new Exception('HTTP_Exception_404');
}
else {
// дальнейшая обработка данных
}
} catch (Exception $e) {
echo $e->getMessage();
}
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($handle);
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
curl_close($handle);
if($httpCode == 200) {
$data=str_get_html($response);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question