How to echo xml in php?

by wilmer.lemke , in category: PHP General , a year ago

How to echo xml in php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by dana , 5 months ago

@wilmer.lemke 

In PHP, you can echo XML by creating a string containing the XML content and then using the header() function to set the Content-Type to "text/xml" before outputting the string with echo. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<?php
// Set the XML content
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<root>
  <name>John Doe</name>
  <age>30</age>
</root>';

// Set the Content-Type to "text/xml"
header('Content-Type: text/xml');

// Output the XML content
echo $xml;
?>


This code will output the XML content and set the Content-Type header to make sure it is interpreted as XML by the browser.