Search code examples
phpxmlsimplexml

Why does libxml_use_internal_errors(true) not respect LIBXML_NOWARNING?


I am tying to understand how libxml_use_internal_errors(true) works. It does not seem to respect the LIBXML_NOWARNING flag.

Here is an example using PHP 7.2.4.

libxml_use_internal_errors( false );
new SimpleXMLElement(
  '<DataSet xmlns="iminvalid"></DataSet>', 
  LIBXML_NOWARNING );
var_dump(sizeof(libxml_get_errors()));
// int(0)

As expected, there are no warnings spit out by these statements. The namespace warning is hidden because of LIBXML_NOWARNING.

However, this:

libxml_use_internal_errors( true );
new SimpleXMLElement(
  '<?xml version="1.0" encoding="utf-8"?><DataSet xmlns="iminvalid"></DataSet>', 
  LIBXML_NOWARNING );
var_dump(sizeof(libxml_get_errors()));

// int(1)
// xmlns: URI iminvalid is not absolute

Unexpectedly returns the invalid uri warning. How is it possible to libxml_use_internal_errors( true ) and hide xml parsing warnings?


Solution

  • As of php 7.3.15 it seems to work correctly. Both examples display no warning, but the second example does capture the warning. The flag does not prevent the capture of the warnings via the function, it only suppresses the warnings from being displayed on the console or web browser.