How to echo XML file in PHP

PHPServer Side ProgrammingProgramming

HTTP URLs can be used to behave like local files, with the help of PHP wrappers. The contents from a URL can be fetched through the file_get_contents() and it can be echoed. or read using the readfile function.

Below is a sample code to do the same −

$file = file_get_contents('');
echo $file;

An alternative is demonstrated below −

header('Content-type: text/xml'); //The correct MIME type has to be set before displaying the output.

The asXML method also can be used. Below is a sample code −

echo $xml->asXML();
$xml->asXML('filename.xml'); //providing a name for the xml file.
Published on 07-Apr-2020 15:03:20