Nowadays web services are 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 you read read XML in 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
|
$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 .= ‘<CategoryName>Wordpress</CategoryName>’;
$xml .= ‘</category>’;
$xml .= ‘</categories>’;
|
Next, main part of code is header, need to write header to read xml content.To display XML file on browser use Content-type: text/xml
header.
1
2
3
4
5
6
7
|
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.