3

is it possible to put PHP code inside a XML document to be later executed? For example, can I say <element value="<?php echo date('Y'); ?>" /> ?

thanks!

  • Ok I've answered my own question...Please see updated question. –  Apr 05 '11 at 09:20
  • 1
    please post your solution as an answer and accept it, or accept one of the others' answers. – kapa Apr 05 '11 at 09:59
  • possible duplicate of [how to add php tags with the dom parser](http://stackoverflow.com/questions/4247726/how-to-add-php-tags-with-the-dom-parser) – Gordon Apr 05 '11 at 10:08

7 Answers7

8

It is possible, but I'd recommend putting it inside of a CDATA block, so you don't have to escape < and similar characters:

<element> 
   <![CDATA[ 
      <?php echo date('Y'); ?>
   ]]>
</element>
darioo
  • 46,442
  • 10
  • 75
  • 103
4

If you run the XML file as a PHP script, then yes. The output of the script will be valid XML, but the file that contains the inline PHP will most likely not be valid XML.

Daniel Dinu
  • 1,783
  • 12
  • 16
3

To force your webserver to execute PHP inside XML files, change the MIME type of .xml-files to application/x-httpd-php, by adding this line to .htaccess:

AddType application/x-httpd-php .xml

Now all XML files are treated by the webserver the same way as PHP files:

<?xml version="1.0" encoding="UTF-8"?>
<servertime> 
  <?php
    echo date('c');
    header('Content-Type: application/xml; charset=utf-8');
  ?>
</servertime>

Note that the header-definition is there to correct the MIME type of your file back to application/xml. Otherwise it would be treated by the browser as HTML, preventing it from displaying a nice DOM tree (or whatever the default XML handling may be...). Even if the file is not intended to be opened in a browser, it's never nice to fake the MIME type of a file, so please don't forget this line ;)

The drawback of this solution: Now all XML files in the access range of your .htaccess will be treated as php, so you'd have to change the MIME-type of any XML file in the same folder via PHP. Workaround: Move your php-hacked XML and the .htaccess to a separate folder.

klamann
  • 1,697
  • 17
  • 28
1

A PHP program can output any kind of content you like, however it will (if used with a web module like mod_php) output with a text/html content type by default, so you will need to do:

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

(Substitute a more appropriate content-type if one is applicable).

You will also need to configure your webserver to recognise that the file should be treated as PHP and not served up as static data. This is usually achieved by giving the file a .php extension. Consult the manual for your webserver and PHP's module for it if you want to use a different file naming convention.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Of course you can. You could have something like: php -f yourfile.xml.

Ali
  • 3,568
  • 2
  • 24
  • 31
0

Yes.

Use this example code, filenamed 'example.php':

<?php
ob_start();
require_once "relative/path/to/file.xml";
$xml_with_php_values = ob_get_clean();
?>

Put the php in the xml file as how you put in html.

nicola
  • 2,141
  • 1
  • 19
  • 19
-1
<element value="&lt;?php echo date('Y'); ?&gt; />