Nowadays web services become more and more popular and Most commonly used format for exchanging information in web services are XML because XML is simple to use and easy to read tag structures.
In this tutorial, I will show you How to display XML content with php using header content type.The header informs the browser which type of document it is so the browser can render it as per that.
Let’s understand the code:
First of all, let’s generate XML code.
1 2 3 4 5 6 7 8 9 10 11 12 | $xml = '<?xml version="1.0" encoding="iso-8859-1"?>'; $xml .= '<categories><category>'; $xml .= '<ID>1</ID>'; $xml .= '<CategoryName>PHP</CategoryName>'; $xml .= '</category>'; $xml .= '<category>'; $xml .= '<ID>2</ID>'; $xml .= '</category>'; $xml .= '</categories>'; |
Next, the main part of a code is a header, need to write a header to read XML content.To display XML file on browser use Content-type: text/XML header.
1 2 3 4 5 6 7 8 | header ("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT"); header ("Cache-Control: no-cache, must-revalidate"); header ("Pragma: no-cache"); header ("Content-type: text/xml"); header ("Content-Description: PHP Generated Data" ); echo $xml; |
That’s all there is to it.Anyway, I hope that helps some of you.If you have any questions please let me know via the comments.
Thanks for reading and feel free to share your thoughts! Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.
Comments (1)