Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP simplexml_load_file warns "failed to load external entity"

ErrorI/O warning : failed to load external entity

Short answer

This warning means libxml could not open the file or URL you passed to simplexml_load_file. Turn on libxml internal errors to see the real reason, then check the path, whether PHP may open URLs at all, and whether the remote server refuses PHP's request. The function returns false, so test with ===.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Collect the libxml errors yourself. The message then names the exact file or URL and what went wrong.

    libxml_use_internal_errors(true);
    $xml = simplexml_load_file($source);
    if ($xml === false) {
        foreach (libxml_get_errors() as $error) {
            echo trim($error->message), PHP_EOL;
        }
        libxml_clear_errors();
    }
  2. 2Match what you see, then jump to its fix

    1. 1The printed name is a short or relative path such as data/feed.xmlThe file path is wrong or relative to the wrong folderFix
    2. 2The source is a URL and the same file loads fine when you save it firstPHP is not allowed to open the http:// addressFix
    3. 3The same URL works in a browser but not from PHPThe remote server answers with an error statusFix

What does the error look like?

  • I/O warning : failed to load external entity
  • simplexml_load_file() returns false

Cause 1 of 3

The file path is wrong or relative to the wrong folder

1Check

A relative path is resolved against the current working directory of the PHP process, which is not always the folder of your script. Print it, and check that the file exists at that path.

php -r 'echo getcwd(), PHP_EOL;'
ls -l /var/www/app/data/feed.xml

2Fix

Build the path from the script location so it never depends on the working directory.

$xml = simplexml_load_file(__DIR__ . '/data/feed.xml');

3Verify

Run the script from another directory. It still loads the file.

cd / && php /var/www/app/read-feed.php

Confirmed by php.net

Did this fix it?

Cause 2 of 3

PHP is not allowed to open the http:// address

1Check

Opening a URL goes through the URL-aware fopen wrappers. allow_url_fopen enables them, it defaults to on, and it can only be changed in php.ini, not with ini_set().

php -r 'var_dump(ini_get("allow_url_fopen"));'

2Fix

Either turn allow_url_fopen on in the php.ini your server loads, or download the XML yourself with an HTTP client such as cURL and parse it with simplexml_load_string().

$ch = curl_init('https://example.com/feed.xml');
curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true]);
$body = curl_exec($ch);
$xml = simplexml_load_string($body);

3Verify

The variable is a SimpleXMLElement and not false.

php -r 'var_dump(ini_get("allow_url_fopen"));'

Confirmed by php.net

Did this fix it?

Cause 3 of 3

The remote server answers with an error status

1Check

With ignore_errors at its default of false, PHP does not fetch the content of a failure status code, so a 403 or 404 ends as a failed load. Look at the status the server returns for a plain request.

curl -sI https://example.com/feed.xml

2Fix

If the status shows the server rejects PHP's default request, send an explicit User-Agent through a stream context (the user_agent option). Set ignore_errors to true only while debugging, to read the error body.

$context = stream_context_create([
    'http' => ['user_agent' => 'MyApp/1.0'],
]);
$raw = file_get_contents('https://example.com/feed.xml', false, $context);
$xml = $raw === false ? false : simplexml_load_string($raw);

3Verify

The request now returns the XML body.

php read-feed.php

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Always test the result with === false, because the function can return false or a falsy object.
  • Use libxml_use_internal_errors(true) in code that reads feeds, and log the messages.
  • Fetch remote XML with an HTTP client that has timeouts, then parse the string.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.