PHP simplexml_load_file warns "failed to load external entity"
I/O warning : failed to load external entityShort 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 ===.
Fix it fast
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(); }2Match what you see, then jump to its fix
- 1
The printed name is a short or relative path such as data/feed.xmlThe file path is wrong or relative to the wrong folderFix - 2
The source is a URL and the same file loads fine when you save it firstPHP is not allowed to open the http:// addressFix - 3
The same URL works in a browser but not from PHPThe remote server answers with an error statusFix
- 1
What does the error look like?
I/O warning : failed to load external entitysimplexml_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.xml2Fix
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.phpConfirmed by php.net
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
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.xml2Fix
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.phpConfirmed by php.net
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
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix