Maaz Zindani

Loading the index…

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

A PHP script's XML output fails with extra content at the end

Errorerror on line 2 at column 1: Extra content at the end of the document

Short answer

An XML document has exactly one root element, and only comments, processing instructions or whitespace may follow it. The error means your PHP script printed something else after the root: a second XML block, a PHP warning, or leftover HTML. Save the raw response and read its end.

  • 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

    Download the raw response and read its end. Whatever comes after the closing root tag is the cause.

    curl -s https://example.com/feed.php | tail -n 15
  2. 2Match what you see, then jump to its fix

    1. 1The raw output shows a second top-level tag, often inside a loopMore than one root element is printedFix
    2. 2Text such as Warning: or Deprecated: appears before or after the XML in the raw responseA PHP warning or notice is printed into the XMLFix
    3. 3The text after the closing root tag is HTML or a debug valueOutput after the XML, such as an HTML footer or a debug printFix

What does the error look like?

  • error on line 2 at column 1: Extra content at the end of the document

Cause 1 of 3

More than one root element is printed

1Check

A well-formed document has one root element and nothing but Misc content (comments, processing instructions, whitespace) after it. A loop that echoes a full item with its own root for each row breaks that rule.

curl -s https://example.com/feed.php | grep -c "<?xml"

2Fix

Open the root element once, print the items inside it, and close it once.

<?php
header('Content-Type: application/xml; charset=UTF-8');
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<items>';
foreach ($rows as $row) {
    echo '<item>' . htmlspecialchars($row['name'], ENT_XML1) . '</item>';
}
echo '</items>';

3Verify

The first command should now print 1, and the browser should render the tree.

curl -s https://example.com/feed.php | grep -c "<?xml"

Confirmed by w3.org

Did this fix it?

Cause 2 of 3

A PHP warning or notice is printed into the XML

1Check

display_errors decides whether errors are printed as part of the output. The default is On, and the bundled php.ini-production sets it to Off.

php -r "echo ini_get('display_errors');"

2Fix

Fix the code that raises the message. Then turn display errors off for the site and send them to the log, which is what the manual advises for production.

; php.ini
display_errors = Off
log_errors = On

3Verify

Fetch the feed again. The response should contain only XML.

curl -s https://example.com/feed.php | head -c 200

Confirmed by php.net

Did this fix it?

Cause 3 of 3

Output after the XML, such as an HTML footer or a debug print

1Check

Search the script and the files it includes for echo, print_r or var_dump, and for HTML after the closing PHP tag.

grep -n "var_dump\|print_r\|echo" feed.php

2Fix

Stop the script right after the XML, and if other code might print first, wrap your output in output buffering and discard what came before.

<?php
ob_start();
// ... code that may print something ...
ob_end_clean();          // erase anything printed so far
header('Content-Type: application/xml; charset=UTF-8');
echo $xml;
exit;

3Verify

The end of the raw response is now the closing root tag.

curl -s https://example.com/feed.php | tail -n 3

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Build the XML in one string or with DOMDocument, and print it in a single place.
  • Keep display_errors off on live sites and read the error log instead.
  • Add a test that fetches the feed and parses it, so a stray echo fails the test.

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.