I produced xml file in my directory. I want to show this file in browser using php?
(I want to show that as sitemap)
Here is my code :
public function siteMap()
{
$test_array = array (
'bla' => 'blub',
'foo' => 'bar',
'another_array' => array (
'stack' => 'overflow',
),
);
$xml_template_info = new \SimpleXMLElement("<?xml version=\"1.0\"?><template></template>");
$this->array_to_xml($test_array,$xml_template_info);
$xml_template_info->asXML(dirname(__FILE__)."/sitemap.xml") ;
header('Content-type: text/xml');
dd(readfile(dirname(__FILE__)."/sitemap.xml"));
}
public function array_to_xml(array $arr, \SimpleXMLElement $xml)
{
foreach ($arr as $k => $v) {
is_array($v)
? $this->array_to_xml($v, $xml->addChild($k))
: $xml->addChild($k, $v);
}
return $xml;
}
And here is sitemap.xml:
<?xml version="1.0"?>
<template><bla>blub</bla><foo>bar</foo><another_array><stack>overflow</stack></another_array></template>
I want show this xml file in my browser, Any idea for doing this correctly?
EDITED : Here is result with header('Content-type: text/plain');

It consist of some extra reports.

header('Content-type: text/plain');should work!</template>tag is closed. Nothing will display it as XML, because it's not valid XML. Something is sending the extra data! Perhaps try anexit();after thereadfilecall.