Getting Error "Below is a rendering of the page up to the first error."

Getting Error "Below is a rendering of the page up to the first error."

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

Stefan Bogdanescu

Founder & Senior Architect

The Content-Type Header and PHP Output Buffering

When generating XML responses in PHP, mixing output buffering behavior with HTTP headers can lead to confusing browser errors. A common scenario involves using XMLWriter to construct a document in memory and then attempting to send it as the HTTP response body. However, if you introduce any echo statements before setting the Content-Type header, or if the XML declaration is generated incorrectly, browsers will fail to parse the response.

The specific error message "Document is empty" or "XML Parsing Error: not well-formed" often stems from a fundamental misunderstanding of how PHP sends data to the client versus how it stores data in memory.

The Problem with Pre-Header Output

In the first code snippet provided, the script initializes the XMLWriter, generates the XML string using $writer->outputMemory(true), and then echoes that specific variable. Crucially, this happens after the header is set. In this flow, the browser receives only the XML content. However, in the second scenario, the code executes echo $time immediately after initializing the script but before setting the Content-Type header.

This creates an immediate conflict. When PHP encounters any output (like echoing a timestamp) before sending headers, it starts writing directly to the output buffer. Once data is flushed to the buffer, you can no longer set HTTP headers cleanly. While this specific issue usually triggers a "Headers already sent" warning in standard PHP environments, browser behavior varies.

In Chrome, if the initial output causes the header negotiation to fail or if the stream is treated differently, the browser might interpret the subsequent XML generation as part of a broken stream, resulting in the "Document is empty" rendering error because the parser expects a clean start based on the declared text/xml content type but receives a mismatched state.

In Mozilla Firefox, the issue manifests differently due to strict XML parsing rules. The browser attempts to parse the response as XML immediately upon receiving bytes. If the response starts with 1354510317 (the timestamp), the parser sees invalid characters at the root level where it expects an XML declaration or a start tag. This results in the "not well-formed" error, specifically pointing to column 11 where the timestamp string ends and the actual PHP-generated content might begin or where the expectation of valid XML structure is violated.

The Role of the XML Declaration

A critical detail often overlooked in these scenarios is how the XMLWriter handles the XML declaration (<?xml version="1.0"?>). By default, when using $writer->startDocument('1.0'), the writer prepares to output this declaration. However, when you call $writer->outputMemory(true), it returns the string without flushing the internal buffer immediately in a way that guarantees the declaration is prepended if not handled correctly in specific contexts.

More importantly, the order of operations dictates success. If you echo raw data (like $time) before calling header("Content-Type: text/xml;"), PHP has already committed to writing data. The browser's parser then receives a stream that begins with non-XML characters. Even if the Content-Type header is eventually set, the parser has already begun processing the stream based on the initial bytes it received.

Correct Implementation Strategy

To resolve this, you must ensure that no output occurs before the headers are sent, and that the XML content is returned cleanly. The XMLWriter class allows you to generate a string in memory without writing directly to the output stream until explicitly told to do so.

The following approach ensures the XML declaration is included in the final string and prevents any interference from preceding echo statements:

<?php
header("Content-Type: text/xml; charset=utf-8"); // Set header first

$writer = new XMLWriter();
$writer->openMemory();
$writer->setIndent(4);
$writer->startDocument('1.0');
$writer->startElement('epp');
    $writer->startElement("command");
        $writer->startElement("login");
            $writer->writeElement('clID', 'hello');
            $writer->writeElement('pw', 'abcdefg');
        $writer->endElement(); // login
    $writer->endElement();    // command
$writer->endElement();       // epp
$writer->endDocument();

$xmlString = $writer->outputMemory(true);

// Do not echo the raw string here if you want to return it via HTTP response
// Instead, rely on the output buffering or explicit printing after headers are set.
echo $xmlString;
?>

If you need to debug the content before sending it (as seen in the user's attempt with echo $time), you must remove that line entirely from the production flow or move it inside a conditional block that only runs during local debugging, ensuring headers are still sent first. Alternatively, capture the timestamp into a variable and access it within the XML structure itself if debugging data is required in the payload, rather than outputting it as raw text to the response stream.

Summary of Best Practices

  1. Headers First: Always call header() before any echo or print statements.
  2. Memory Buffering: Use $writer->openMemory() and $writer->outputMemory(true) to construct the XML string safely without immediate output interference.
  3. Clean Output: Ensure no raw text (like timestamps) is echoed before the XML string is generated or sent.
  4. Charset Specification: It is best practice to include ; charset=utf-8 in your Content-Type header to ensure consistent parsing across different browsers.

By adhering to this order of operations, you prevent the parser from encountering non-XML data at the start of the stream, thereby eliminating the "Document is empty" and "not well-formed" errors.

Our network