How to echo xml in php?

by jasen_gottlieb , in category: PHP General , 6 months ago

How to echo xml in php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by aubrey , a month ago

@jasen_gottlieb 

To echo XML in PHP, you can use the header() function to set the content type to text/xml and then use the echo or print function to output the XML content. Here is an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<?php
// Set the content type to XML
header('Content-type: text/xml');

// Create XML content
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<root>
  <title>Hello World</title>
  <message>Welcome to the world of XML</message>
</root>';

// Output XML content
echo $xml;
?>


This code will output the XML content with the appropriate content type header. You can customize the XML content based on your requirements.