php - Handle errors in simple html dom -
i have code public available data fetching website
//array of params foreach($params $par){ $html = file_get_html('website.com/$par'); $name = $html->find('div[class=name]'); $link = $html->find('div[class=secondname]'); foreach($link $i => $result2) { $var = $name[$i]->plaintext; echo $result2->href,"<br>"; //insert database } }
so goes given website different parameter in url each time on loop, keep getting errors breaks script when 404 comes or server temporarily unavailable. have tried code check headers , check if $html object first still errors, there way can skip errors , leave them out , carry on script?
code have tried checked headers
function url_exists($url){ if ((strpos($url, "http")) === false) $url = "http://" . $url; $headers = @get_headers($url); //print_r($headers); if (is_array($headers)){ //check http error here....should add checks other errors too... if(strpos($headers[0], '404 not found')) return false; else return true; } else return false; }
code have tried check if object
if (method_exists($html,"find")) { // check if html element exists avoid trying parse non-html if ($html->find('html')) { // , start searching (and manipulating) dom
you need more specific, kind of errors getting? line errors out?
edit: since did specify errors you're getting, here's do:
i've noticed you're using single quotes string contains variables. won't work, use double quotes instead, i.e.:
$html = file_get_html("website.com/$par");
perhaps issue?
also, use file_get_contents()
if (file_get_contents("website.com/$par") !== false) { ... }
Comments
Post a Comment