Output raw XML using php

Output raw XML using php

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

Stefan Bogdanescu

Founder & Senior Architect

Why Raw XML Output Often Requires Explicit Configuration

The scenario you described—printing a simple PHP string and seeing only the text content of the inner elements—is a classic symptom of how modern browsers handle HTML documents. When a browser receives a response without explicit instructions that it is receiving raw data (like XML) rather than an HTML document, it defaults to rendering mode. In this mode, the browser parses the input as if it were an HTML page. It strips out tags that have no corresponding HTML elements or treats them as text nodes depending on the context, often hiding the <root> and </root> tags from view because they aren't part of the Document Object Model (DOM) in a way that renders visually like a paragraph.

To get the full XML structure visible in Chrome or Firefox, you must tell the browser explicitly what kind of data it is receiving. You do this by sending the correct HTTP content type header. Without this header, PHP's print() function outputs the string correctly to the output buffer, but the browser interprets that buffer as an HTML file.

Here is how to fix the script immediately:

<?php
header("Content-Type: text/xml");

$output = "<root><name>sample_name</name></root>";
print($output);
?>

By adding header("Content-Type: text/xml"); before any output is sent, you change the browser's behavior. The browser now knows to treat the response body as a raw XML document. This forces it to display the tags and formatting exactly as defined in your string, rather than parsing them as HTML or stripping them away.

Formatting and Pretty-Printing

While the header fix ensures the XML is visible, the output will appear on a single line: <root><name>sample_name</name></root>. If you prefer to see the multi-line format shown in your example, you need to add whitespace characters (spaces or newlines) directly into the string variable.

Browsers generally ignore extra whitespace for data transmission purposes, but for debugging and readability in a browser window, it is useful to include them:

<?php
header("Content-Type: text/xml");

// Adding spaces and newlines for visual clarity
$output = "<root>\n    <name>sample_name</name>\n</root>";
print($output);
?>

Notice the \n and the indentation spaces. These are literal characters inside the PHP string. When the browser receives this, it renders them exactly as they appear in the source code, providing a clean, indented view that is much easier to read when inspecting network responses or debugging API data.

Avoiding HTML Escaping

A common pitfall when trying to output XML from PHP is accidentally escaping the characters. In your original example, you used print ($output);. If $output contained quotes or special characters, you might be tempted to echo it using echo $output; without care, or worse, concatenate variables that haven't been sanitized.

However, when outputting raw XML, you must ensure the content is not being HTML-escaped by PHP itself. The print() function outputs the variable exactly as it exists in memory. If you were to use var_dump($output) instead, you would see the escaped version (&lt;root&gt;). Always remember that echo and print are for raw output; htmlspecialchars or htmlentities are for escaping content for HTML display. Since you want to show the XML tags, do not apply HTML escaping functions to your XML string.

Server Configuration Considerations

In some rare cases, web server configurations might override PHP's headers. For instance, if your .htaccess file or nginx configuration forces a specific content type for all requests, your PHP header() call might be ignored or placed in the wrong order. The HTTP protocol requires that headers must be sent before any body content (including whitespace).

If you find that adding the header doesn't work immediately, check if there is any output buffering active. Sometimes, a preceding echo or even accidental whitespace after the opening <?php tag can cause headers to fail. Ensure your script starts cleanly:

<?php
// No whitespace before <?php
header("Content-Type: text/xml");
// ... rest of code
?>

Why This Matters for APIs

This distinction is critical when building APIs, such as the Google Weather API you referenced. Those services rely on clients checking the Content-Type header to determine how to parse the response. If your script returns XML but the header says text/html, client libraries might try to render it as a webpage or fail to parse the data structure correctly.

For developers using frameworks like Laravel, this is handled automatically when returning responses from controllers, but in vanilla PHP scripts, you must be explicit. If you are building an API endpoint that supports both JSON and XML, ensure you set the header dynamically based on the Accept header sent by the client or a query parameter, ensuring the correct MIME type is returned for every request.

For more structured handling of responses within a Laravel application, consider using the Response facade to manage content types reliably across your entire stack.

Our network