SimpleXML - I/O warning : failed to load external entity

SimpleXML - I/O warning : failed to load external entity

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

SimpleXML - I/O warning : failed to load external entity

When parsing an RSS feed in a web context, the error I/O warning : failed to load external entity often stems from a misunderstanding of how PHP's input streams interact with HTTP headers and caching layers. The code snippet you are attempting runs into this specific trap because simplexml_load_file() is being handed a string that has already been processed by file_get_contents(), but the underlying stream wrapper or internal buffering is misinterpreting the data source.

In your specific case, using file_get_contents('http://feeds.bbci.co.uk/...') followed immediately by passing that result to simplexml_load_file() creates a double-handling scenario that triggers warnings in certain PHP configurations. The first line retrieves the content into memory as a string. However, when simplexml_load_file() receives this string, it attempts to treat the input as if it were a file path or a fresh URL stream. If your server environment has strict security settings or if the variable contains embedded whitespace or BOM (Byte Order Mark) characters from the HTTP response, the parser fails to recognize the XML declaration at the start of the buffer.

The error message failed to load external entity "<?xml version="1.0" indicates that the parser is seeing a malformed entity reference rather than the expected raw bytes. This usually happens when the data passed to the function isn't truly a "file" or a direct URL, but a string that lacks the proper stream context required by simplexml_load_file().

To resolve this, you should bypass the intermediate step of loading the content into a variable if your goal is solely to parse it. Instead, pass the URL directly to simplexml_load_string() after ensuring the data is clean, or better yet, use simplexml_load_file() with the direct URL string but ensure no whitespace precedes it. However, the most robust approach in modern PHP applications is to fetch the content into a variable and explicitly cast it as a string for simplexml_load_string(), which handles the input format more predictably than the file-based loader when dealing with network responses.

Here is a corrected implementation pattern that avoids the entity parsing issue:

$feedUrl = 'http://feeds.bbci.co.uk/sport/0/football/rss.xml?edition=int';
$xmlContent = file_get_contents($feedUrl);

if ($xmlContent === false) {
    // Handle network error or HTTP failure here
    die('Failed to fetch RSS feed');
}

// Use simplexml_load_string instead of passing the URL again
$items = simplexml_load_string($xmlContent);

if (!$items) {
    $errors = libxml_get_errors();
    foreach ($errors as $error) {
        echo $error->message;
    }
}

This approach ensures that file_get_contents() handles the HTTP request and headers, returning a raw string. By passing that string to simplexml_load_string(), you prevent the function from attempting to re-resolve the URL or interpret the input as a file path, which eliminates the "external entity" warning.

If you must stick with simplexml_load_file(), ensure that allow_url_fopen is enabled and that your php.ini settings do not restrict remote file access. However, relying on libxml_disable_entity_loader(false) might be necessary in older PHP versions to prevent the parser from treating external entities as a security risk, though this defaults to true in modern versions by default for safety.

Another common pitfall involves the presence of a Byte Order Mark (BOM) at the beginning of the fetched content. Some feed providers include invisible characters before the XML declaration. You can strip these using trim() or regular expressions before parsing:

$xmlContent = preg_replace('/\xEF\xBB\xBF/', '', $xmlContent);
$items = simplexml_load_string($xmlContent);

For developers working within the Laravel ecosystem, similar logic applies when utilizing packages like spatie/laravel-feed. While those abstractions handle the fetching internally, understanding the underlying stream issues helps when debugging raw XML parsing failures. If you are integrating custom feed parsers into a larger application, consider using DOMDocument or XmlParser for stricter validation needs.

Ultimately, the key is matching the input type to the function signature. simplexml_load_file() expects a path or URL that can be opened as a stream, while simplexml_load_string() expects a raw string. Mixing these expectations without accounting for HTTP response handling leads directly to the I/O warnings you encountered.

Our network