A PHP script's XML output fails with extra content at the end
error on line 2 at column 1: Extra content at the end of the documentShort 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.
Fix it fast
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 152Match what you see, then jump to its fix
- 1
The raw output shows a second top-level tag, often inside a loopMore than one root element is printedFix - 2
Text 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
The text after the closing root tag is HTML or a debug valueOutput after the XML, such as an HTML footer or a debug printFix
- 1
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
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 = On3Verify
Fetch the feed again. The response should contain only XML.
curl -s https://example.com/feed.php | head -c 200Confirmed by php.net
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.php2Fix
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 3Confirmed by php.net
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
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