1

I have search through countless pages trying to find an answer that actually works. I have tried libraries files to specifically deal with warning and error handling, yet even when I suppress all warnings and errors, this one final warning is still showing:

Warning: DOMDocument::loadHTML(): Empty string supplied as input

My php processing is below. The code works perfectly as long as the user enters an actual url, however when the user inputs data that is not a url the warning above is displayed.

if (isset($_GET[article_url])){
    $title = 'contact us';
    $str = @file_get_contents($_GET[article_url]);
    $test1 = str_word_count(strip_tags(strtolower($str)));
    if($test1 === FALSE) { $test = '0'; }
    if ($test1 > '550') {
        echo '<div><i class="fa fa-check-square-o" style="color:green"></i> This article has '.$test1.' words.';
    } else {
        echo '<div><i class="fa fa-times-circle-o" style="color:red"></i> This article has '.$test1.' words. You are required to have a minimum of 500 words.</div>';
    }

    $document = new DOMDocument();
    $libxml_previous_state = libxml_use_internal_errors(true);
    $document->loadHTML($str);
    libxml_use_internal_errors($libxml_previous_state);

    $tags = array ('h1', 'h2');
    $texts = array ();

    foreach($tags as $tag)
    {
        $elementList = $document->getElementsByTagName($tag);
        foreach($elementList as $element)
        {
            $texts[$element->tagName] = strtolower($element->textContent);
        }
    }

    if(in_array(strtolower($title),$texts)) {
        echo '<div><i class="fa fa-check-square-o" style="color:green"></i> This article used the correct title tag.</div>';
    } else {
    echo "no";
    }
}

How can I suppress this warning?

It seems the suggestion seems to be stop suppressing warnings and instead fix them, so I am listing all the warnings when I stop suppressing them

Warning: DOMDocument::loadHTML(): htmlParseEntityRef: expecting ';' in Entity
Warning: DOMDocument::loadHTML(): htmlParseStartTag: misplaced <body> tag in Entity
Warning: DOMDocument::loadHTML(): Tag header invalid in Entity
Warning: DOMDocument::loadHTML(): Tag section invalid in Entity
Warning: DOMDocument::loadHTML(): error parsing attribute name in Entity
Warning: DOMDocument::loadHTML(): Tag footer invalid in Entity
Warning: DOMDocument::loadHTML(): htmlParseEntityRef: no name in Entity
DOMDocument::loadHTML(): Unexpected end tag : strong in Entity

Keep in mind I am scanning user input url's so I have no control over the format of the page being tested - meaning I can't fix their code.

So what do I do if not suppress warnings?

Bruce
  • 1,039
  • 1
  • 9
  • 31
  • 1
    Rather than suppress warnings, write code that checks things are set. `if (!empty($str)) { do your stuff }` or `if ($str != '') { ... }` – Ronnie Oct 27 '16 at 23:23
  • 1
    `article_url` should be in quotes. If you suppress the warning isn't your page just going to do nothing? – chris85 Oct 27 '16 at 23:28
  • `_+1` about chris85 quotes comment, remove the @ before `file_get_contents` to see if there's a problem. – Casimir et Hippolyte Oct 27 '16 at 23:50
  • @Ronnie I have also tried that method, and it suppresses all warnings except the one I am displaying.... – Bruce Oct 27 '16 at 23:51
  • @chris85 no suppressing warnings, will suppress warnings. And in the event that someone submitted a non-url in a url input.... that would mean it does nothing... but what else would it do if the user enters a non-url... clearly it can't do something. – Bruce Oct 27 '16 at 23:54
  • @CasimiretHippolyte what kind of problem would their be? The script runs perfectly I just need to stop outputting a warning when the user enters a non-url... and instead display nothing. – Bruce Oct 27 '16 at 23:55
  • as far as the quotes comment, under strictest rules, yes it should have quotes, though if you understand why, you will also understand when they are not needed. If there is no reason to distinguish between a string and a constant, the quotes really are not needed. Equally one could opt to never use quotes, and convert all variables to strings using ToString. – Bruce Oct 27 '16 at 23:58
  • I also attempted to use the class in the accepted answer on http://stackoverflow.com/questions/1148928/disable-warnings-when-loading-non-well-formed-html-by-domdocument-php - same results. Always displays this warning. – Bruce Oct 28 '16 at 00:01
  • seriously..don't suppress warnings..They are warning you for a reason. If you implemented the if correctly, you would not still be getting a warning – Ronnie Oct 28 '16 at 00:03
  • @Ronnie okay if I am not suppressing how would I prevent the warning of headers already declared? I am loading a page as a document so I can check the page for tags and test against variables and the likes. If I exclude the headers (thus preventing that particular warning) then I can't test for title tag of the document, or meta tags.... but there is already a header in the page I am testing from.... so suppress warnings, Equally the warning I am asking about here... only happens if the user enters a string other than a url. Obviously a new document can;t be created from a non-url.... warning. – Bruce Oct 28 '16 at 00:11
  • I updated the question to show the warnings I receive if I don't suppress... since I am being told not to suppress what do I do? – Bruce Oct 28 '16 at 00:26
  • I tested this with `error_reporting(E_ALL)` and `ini_set('display_errors',1);` and I am not getting any errors. – Ronnie Oct 28 '16 at 03:50
  • @Ronnie I would love to see what that test looks like, cause that's not working for me. If the url is not a valid url, it spits me a warning 100% of the time. http://new.adsactlyhits.com/includes/article_check.php?article_url=rdfdgdg – Bruce Oct 28 '16 at 06:57
  • I just ran this http://pastie.org/10951811 If article_url is different than an actual url, give me an example of the contents of article_url – Ronnie Oct 28 '16 at 16:07
  • @Ronnie I really think my problem is evading people. First, the user inputs a url. I have zero control over that. So imagine the user input google.com. In that case, my script will run flawlessly, without any errors. HOWEVER if the user shoves in random arbitrary text that is not a url, it spits back errors. Take the very same pastie, and try it using 'abcde' as the url instead of google.com - you will get the same warning I am trying to prevent. – Bruce Oct 29 '16 at 18:01

2 Answers2

4

Alright @Bruce..I understand the issue now. What you want to do is test the value of file_get_contents()

<?php
error_reporting(-1);
ini_set("display_errors", 1);

$article_url = 'http://google.com';
if (isset($article_url)){
  $title = 'contact us';
  $str = @file_get_contents($article_url);
  // return an error
  if ($str === FALSE) {
    echo 'problem getting url';
    return false;
  }

  // Continue
  $test1 = str_word_count(strip_tags(strtolower($str)));
  if ($test1 === FALSE) $test = '0';

  if ($test1 > '550') {
    echo '<div><i class="fa fa-check-square-o" style="color:green"></i> This article has ' . $test1 . ' words.';
  } else {
    echo '<div><i class="fa fa-times-circle-o" style="color:red"></i> This article has ' . $test1 . ' words. You are required to have a minimum of 500 words.</div>';
  }

  $document = new DOMDocument();
  $libxml_previous_state = libxml_use_internal_errors(true);
  $document->loadHTML($str);
  libxml_use_internal_errors($libxml_previous_state);

  $tags = array ('h1', 'h2');
  $texts = array ();

  foreach($tags as $tag) {
    $elementList = $document->getElementsByTagName($tag);
    foreach($elementList as $element) {
      $texts[$element->tagName] = strtolower($element->textContent);
    }
  }

  if (in_array(strtolower($title),$texts)) {
    echo '<div><i class="fa fa-check-square-o" style="color:green"></i> This article used the correct title tag.</div>';
  } else {
    echo "no";
  }
}
?>

So if ($str === FALSE) { //return an error } and don't let the script continue. You could return false like I am doing or just do an if/else.

Ronnie
  • 11,138
  • 21
  • 78
  • 140
0
return $this->subject($data['subject'])->markdown('mails.send_instant_notification', compact('data'));

If you are using markdown as template build, do not include inline styling. But if you're using view you can include inline css styling.

return $this->subject($data['subject'])
                ->view('mails.send_instant_notification', compact('data'));
Codedreamer
  • 1,552
  • 15
  • 13