PHP libxml_set_external_entity_loader() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
libxml

Definition

In PHP 8.2 and later, libxml_set_external_entity_loader() lets you register your own function that decides how libxml loads external entities and DTDs.

Instead of letting libxml fetch files or URLs on its own, your callback receives the request and can block it, redirect it to a safe local file, or return nothing. Pass null to go back to the default loader.

Syntax

Call libxml_set_external_entity_loader() with the values you need. Start with this line.

PHP
libxml_set_external_entity_loader(function ($public, $system, $context) { return null; });
CallWhat it does
libxml_set_external_entity_loader(fn($p, $s, $c) => null)Registers a loader that blocks every external entity and returns true.
libxml_set_external_entity_loader(fn($p, $s, $c) => '/safe/file.dtd')Redirects every request to a trusted local file.
libxml_set_external_entity_loader(null)Restores the default libxml loader and returns true.

Available as of PHP 8.2.0. The callback receives three arguments: the public ID (string or null), the system ID (the URL or path), and a context array. It should return a stream resource, a file path string, or null to refuse the request.

Library declarations
PHP
libxml_set_external_entity_loader(?callable $resolver_function): bool

Parameters

ParameterDescription
resolver_functionRequired. A callable with the signature function(?string $public, string $system, array $context) that returns a resource, a string path, or null. Pass null to restore the default loader.

Return Value

Return ValueDescription
boolReturns true on success.

PHP Examples

Example 1 — Block Every External Entity

Register a loader that returns null so no external entity is ever loaded.

PHP
<?php
libxml_use_internal_errors(true);

$calls = 0;
libxml_set_external_entity_loader(function ($public, $system, $context) use (&$calls) {
    $calls++;
    return null;
});

$xml = '<?xml version="1.0"?>
<!DOCTYPE root [<!ENTITY data SYSTEM "secret.txt">]>
<root>&data;</root>';

$doc = new DOMDocument();
$doc->loadXML($xml, LIBXML_NOENT);

echo $calls > 0 ? "Loader was called: yes" : "Loader was called: no", PHP_EOL;

How It Works

  1. The loader counts how many times libxml asks for an external entity and always returns null.
  2. The XML defines an entity that points to secret. txt.
  3. LIBXML_NOENT asks libxml to expand the entity, so it calls our loader.
  4. Because the loader returns null, nothing from secret. txt is loaded.

Example 2 — Reset to the Default Loader

Pass null to remove your custom loader and use the built-in behavior again.

PHP
<?php
$set = libxml_set_external_entity_loader(fn($public, $system, $context) => null);
var_dump($set);

$reset = libxml_set_external_entity_loader(null);
var_dump($reset);

var_dump(libxml_get_external_entity_loader());

How It Works

  1. The first call registers a closure and returns true.
  2. Passing null is also allowed and returns true.
  3. That second call removes the custom loader.
  4. libxml_get_external_entity_loader() now returns NULL, confirming the default is active.

Example 3 — Serve Entities from a Safe File

Redirect every entity request to a file you control instead of letting the XML choose.

PHP
<?php
$safeFile = tempnam(sys_get_temp_dir(), 'ent');
file_put_contents($safeFile, 'Hello from the safe loader');

libxml_set_external_entity_loader(function ($public, $system, $context) use ($safeFile) {
    return $safeFile;
});

$xml = '<?xml version="1.0"?>
<!DOCTYPE root [<!ENTITY msg SYSTEM "anything.txt">]>
<root>&msg;</root>';

$doc = new DOMDocument();
$doc->loadXML($xml, LIBXML_NOENT);

echo trim($doc->documentElement->textContent), PHP_EOL;

libxml_set_external_entity_loader(null);
unlink($safeFile);

How It Works

  1. We create a temporary file that holds trusted text.
  2. The loader ignores the requested system ID and always returns that safe file path.
  3. Parsing with LIBXML_NOENT expands the entity using our file, not anything. txt.
  4. At the end we reset the loader to null and delete the temporary file.

Frequently Asked Questions

PHP 8.2.0. It is not available in earlier versions, so check with function_exists() if you must support older PHP.
Three: the public ID (string or null), the system ID (usually a path or URL), and a context array with extra information about the request.
A stream resource, a string path to load, or null to refuse. Returning null is the safest way to block external entities.
Call libxml_set_external_entity_loader(null). This restores the default libxml loader.
Yes, for advanced control. libxml_disable_entity_loader() is deprecated in PHP 8.0, and the set and get pair gives you precise handling in PHP 8.2+.

Next: libxml_set_streams_context()

libxml_set_streams_context() sets the stream context that libxml uses when it opens files .

libxml_set_streams_context() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful