Is it not possible to use PHP and grab an XML file?



Why can't I use PHP in an .xml file at the same time grab it with SimpleXMLElement? When one works, the other doesn't.


Example: If I use PHP in the .xml file like


file.xml.php



<?php
require('db.php');
header('Content-Type: application/xml; charset=utf-8');

$updated_date = date('l', filemtime('file.xml.php'));
?>
<?xml version="1.0" encoding="UTF-8" ?>
<content>
<value name="Content 1" val="Value 1"></value>
<value name="Content 2" val="Value 2"></value>
<value name="Content 3" val="Value 3"></value>
<value name="Last Updated" val="<?php echo $updated_date; ?>"></value>
</content>


It works fine. But if I use SimpleXMLElement to get the file, it gives me this error.



PHP Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /var/www/html/get_xml.php:336\nStack trace:\n#0 /var/www/html/get_xml.php(336): SimpleXMLElement->__construct('<?php??require(...')\n#1 {main}\n


I know it's telling me that the xml file cannot be read. However, when I read the xml file through its own URL, it works fine as if it was just another xml file.


Is it not possible to use PHP in .xml files and grab them at the same time? Without PHP, the file is kind of useless to me. Unless if I can just use the plain name so that it doesn't show that error. I know it has to do something with the require.


No comments:

Post a Comment