Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP 8 - libxml_disable_entity_loader() has been deprecated. How do I replace the functionality it provides?

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:

like image 791
Pancho Avatar asked Sep 19 '25 06:09

Pancho


2 Answers

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.

like image 138
Pancho Avatar answered Sep 20 '25 21:09

Pancho


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);
...
like image 45
Nesar Ahmad Nori Avatar answered Sep 20 '25 20:09

Nesar Ahmad Nori