Home  >  Article  >  Backend Development  >  How to output XML file in PHP

How to output XML file in PHP

WBOY
WBOYforward
2023-09-03 17:45:12848browse

How to output XML file in PHP

With the help of PHP wrappers, HTTP URLs can behave like local files. The contents of a URL can be obtained via file_get_contents() and can be echoed. Or read using the readfile function.

Below is sample code to do the same -

$file = file_get_contents('http://example.com/');
echo $file;

An alternative is demonstrated below -

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

The asXML method can also be used. Below is the sample code -

echo $xml->asXML();
or
$xml->asXML('filename.xml'); //providing a name for the xml file.

The above is the detailed content of How to output XML file in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete