I am upgrading to PHP 8 and getting the following warning:
Function libxml_disable_entity_loader() is deprecated
What I have: This code saves the current entity loader status, and enables the loader; then loads the file; and finally resets the entity loader to its original status:
...
$current = libxml_disable_entity_loader(false);
$domdocument->load($filename,$options);
libxml_disable_entity_loader($current);
...
I read the following regarding PHP 8:
as of libxml 2.9.0 entity substitution is disabled by default, so there is no need to disable the loading of external entities, unless there is the need to resolve internal entity references with LIBXML_NOENT.
To test, I removed the libxml_disable_entity_loader() references, thus What I have: becomes:
...
$domdocument->load($filename,$options);
...
However, now of course I get:
PHP Warning: DOMDocument::load(): I/O warning : failed to load external entity
So, my question is:
What do I need to do in PHP 8 to get rid of libxml_disable_entity_loader() and still achieve the equivalent of What I have:
I can't speak for all use cases but in mine all loads are from local files, so I have resolved the problem by replacing ->load() with a combination of file_get_contents() and ->loadXML() as follows:
...
$domdocument->loadXML(file_get_contents($filename),$options);
...
Thus side stepping the "external" consideration.
I have not dug deep into this topic, but I suppressed the warning by adding @ symbol at the beginning of the method and no change has come to the result of the code. try like below
...
$current = @libxml_disable_entity_loader(false);
$domdocument->load($filename,$options);
@libxml_disable_entity_loader($current);
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With