4

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!

3
  • Ok I've answered my own question...Please see updated question. Commented Apr 5, 2011 at 9:20
  • 1
    please post your solution as an answer and accept it, or accept one of the others' answers. Commented Apr 5, 2011 at 9:59
  • possible duplicate of how to add php tags with the dom parser Commented Apr 5, 2011 at 10:08

7 Answers 7

9

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>
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

1 Comment

I do not think this is a good or secure idea.
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.

Comments

0

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

Comments

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.

Comments

-1
<element value="&lt;?php echo date('Y'); ?&gt; />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.