Maaz Zindani

Loading the index…

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

Browser says 'error on line 1 at column 1: Document is empty' for a PHP XML page

ErrorThis page contains the following errors: error on line 1 at column 1: Document is empty

Short answer

This browser message means the PHP script returned no output at all, so there is no XML to parse. Almost always PHP stopped on an error it did not print: a fatal error with display_errors off, or a parse error in the file itself. Read the PHP error log, not the browser.

  • Applies to PHP 8.3, PHP 8.4, Apache, php-fpm
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Request the URL with curl and look at the body and the status code. An empty body with 500 points to a PHP error.

    curl -i https://example.com/feed.php
  2. 2Match what you see, then jump to its fix

    1. 1curl returns status 500 with an empty bodyA fatal error happened and display_errors is OffFix
    2. 2Setting error_reporting or display_errors at the top of the script changes nothingThe file has a parse errorFix
    3. 3simplexml_load_string() gives false and the page ends emptyYour own code reads XML and fails with no detailFix

What does the error look like?

  • This page contains the following errors: error on line 1 at column 1: Document is empty
  • Below is a rendering of the page up to the first error.

Cause 1 of 3

A fatal error happened and display_errors is Off

1Check

display_errors is On by default, but php.ini-production sets it to Off, so errors are not printed and the response is empty. The advice for production is to log errors instead.

php -i | grep -E "display_errors|log_errors|error_log"

2Fix

Turn logging on, read the log, and fix the error it names. Show errors on a development copy only.

; php.ini
log_errors = On
error_log = /var/log/php/error.log

3Verify

Request the URL again and read the last lines of the log.

tail -n 20 /var/log/php/error.log

Confirmed by php.net

Did this fix it?

Cause 2 of 3

The file has a parse error

1Check

PHP parses the whole file before it runs any of it. A missing bracket or semicolon produces a blank page, and ini_set at the top of that same file never runs.

tail -n 5 /var/log/php/error.log

2Fix

Enable error display in php.ini on the development copy, or include the risky file from a small wrapper that sets display_errors first.

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
include 'feed-real.php';

3Verify

Load the wrapper: the parse error now prints on the page.

curl -i https://example.com/feed.php

Confirmed by php.net

Did this fix it?

Cause 3 of 3

Your own code reads XML and fails with no detail

1Check

The function raises an E_WARNING for each XML error and returns false on failure. Collect the errors to see line and column.

grep -R -n "simplexml_load" --include=*.php .

2Fix

Turn on libxml internal errors, test the result with ===, and print the collected messages to the log.

libxml_use_internal_errors(true);
$xml = simplexml_load_string($data);
if ($xml === false) {
    foreach (libxml_get_errors() as $e) {
        error_log(trim($e->message) . ' line ' . $e->line);
    }
}

3Verify

Run the request again and read the log.

tail -n 20 /var/log/php/error.log

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Keep log_errors on in production and read the log after each deploy.
  • Run the feed URL through curl in your deploy checks and test for a non-empty body.

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.