1

I am using the following code to parse definitions from a remote website , according to the usernames in my database. I am using simple html dom parser.

//database connection
include 'db.php';
//display errors
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include 'simple_html_dom.php';

//select usernames order alphabetically
$row = mysqli_query($conn, "SELECT user_name FROM (
  SELECT * 
  FROM store 
  ORDER BY user_id DESC) 
  AS store ORDER BY user_id ASC");

//echo out definitions of usernames
while($lastusers = mysqli_fetch_array($row)) {
    echo '<br>' . $lastusers["user_name"];
    echo '<br>Definition:<br>';
    $html = file_get_html("https://www.merriam-webster.com/dictionary/" . $lastusers["user_name"]);
    $title = $html->find("div.card-primary-content", 0)->innertext;
    echo $title;

    //save definitions to corresponding username 
    $save = mysqli_query($conn, 'UPDATE  store
    SET     def = $title,
    WHERE   user_name = $lastusers["user_name"]'
    )
}

My while loop will begin to generate the definitions per username , until certain pages are not found resulting in an error that stops the loop.

Warning: file_get_contents(https://www.merriam-webster.com/dictionary/colourings): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found in /app/simple_html_dom.php on line 75 Fatal error: Call to a member function find() on boolean in /app/test.php on line 18

My question is how to skip over the username that throws the error and continue the loop? Also because their are over 500 usernames , this operation is very intensive , so i'm saving the definitions , and would like to avoid the errors being saved to the database.

Pacified
  • 165
  • 9
  • 1
    Use a try/catch block inside of your loop. – aynber Aug 22 '17 at 16:43
  • @aynber can't catch what throws no exception in the first place ... this is a simple warning message. – CBroe Aug 22 '17 at 16:44
  • Fatal errors in PHP are not Exceptions – ishegg Aug 22 '17 at 16:44
  • @CBroe Oh, right, good point. – aynber Aug 22 '17 at 16:45
  • You can also suppress errors in PHP (although I wouldn't suggest it) using `@` - see this question for more information about error suppression: https://stackoverflow.com/questions/136899/suppress-error-with-operator-in-php – ctwheels Aug 22 '17 at 16:46
  • Your code marked `save definitions to corresponding username` has several pretty big issues, the query itself isn't surrounded by quotes, you are using string concatenation (should use [Prepared Statements](https://en.wikipedia.org/wiki/Prepared_statement)) AND you are missing the ending parenthesis of `mysqli_query()`. – GrumpyCrouton Aug 22 '17 at 16:48
  • Another way would be to replace `file_get_html` with `file_get_contents`, and passing in an HTTP stream context that has `ignore_errors` set to true (that would avoid the warning AFAIK). Then, check the status code, and if that does not indicate an error, you can feed the HTML code to `str_get_html` to create the parser object and continue from there. – CBroe Aug 22 '17 at 16:49

1 Answers1

0

You can suppress the warning like shown in the following link

How can I handle the warning of file_get_contents() function in PHP?

Or an other alternative is to write a custom error handler. See this link

PHP: How to use set_error_handler() to properly deal with all errors except notices?