Welcome to WebmasterWorld Guest from 54.167.83.224

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Processing .xml files with PHP

     
6:25 pm on Mar 10, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 24, 2003
posts:111
votes: 0


I'm having trouble getting my dynamic XML file to serve as a .xml. I've used PHP to dynamically create a validating XML file except that it's served with a filename like: feed.php. What I want is to rename it as feed.xml and still have PHP pre-process it.

I've researched and determined that I needed to add "AddType application/x-httpd-php .xml" to my .htaccess file, which I've done. Now, I know I have the ability to use .htaccess files in my directories as I use them for mod_rewrites and permissions, but for some reason this does not force PHP to pre-process a file with a .xml extension. However, when I run the .xml feed URL through a validator it tells me "Feeds should not be served with the "application/x-httpd-php" media type", which would seem to indicate that Apache is sending the file out via PHP, but I can see all my PHP coding in the .xml file when I view it in a browser.

Any ideas what I missed?

6:33 pm on Mar 10, 2005 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12533
votes: 0


short_open_tag [php.net] perhaps?
8:21 pm on Mar 10, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 24, 2003
posts:111
votes: 0


Shouldn't be an issue, I'm displaying that line with:
echo "<?xml version=\"1.0\"?>";
Which works fine in the .php file. The problem is that my PHP code is not being processed only when the file has an extension of .xml
8:37 pm on Mar 10, 2005 (gmt 0)

Full Member

10+ Year Member

joined:Mar 8, 2004
posts:311
votes: 0


served with the "application/x-httpd-php" media type", which would seem to indicate that Apache is sending the file out via PHP,

That implies the opposite to me. Apache labels all files you want processed by PHP as application/x-httpd-php, then you tie the application/x-httpd-php media type to the PHP handler. The PHP processor then outputs the real media type that is seen by the client. The default media type is text/html, and you can change this with the PHP script with, eg
header("Content-type: application/xml")

The Web doesn't has no concept of "filename extensions". If the url [example.com...] returns Content-type: application/xml, then that URL points to a XML resource. Anything that tries to guess the filetype by its "extension" is broken. (Yes this includes IE).

9:57 pm on Mar 10, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 24, 2003
posts:111
votes: 0


I already thought of that. The top of my file contains:
<?php
header('Content-type: text/xml');
echo "<?xml version=\"1.0\"?>";
?>

I've also tried application/xml as well and the result is the same.

And my .htaccess file has:
AddType application/x-httpd-php .xml

According to every tutorial I've found and every post I read on WebmasterWorld, everything is set up okay. However, regardless, when I try to view the XML file or validate it the PHP code shows as if it wasn't parsed at all.

5:51 pm on Mar 14, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 24, 2003
posts:111
votes: 0


As a followup, I could find no solution to this. However, the feeds serve fine with a php extension, and have no trouble in news readers. The only problem is cosmetic, that the feeds don't have the .xml or .rss extension. Other than that they function as normal.
10:10 pm on Mar 14, 2005 (gmt 0)

New User

10+ Year Member

joined:Dec 1, 2003
posts:27
votes: 0


I was having a similar situation, but my XML code was not contained in files, it was entered into a "template" form stored in a MySQL database and was parsed as needed.

In my situation my version of PHP was not compiled using the --with-xml option, so it was not set up to process the XML and would give me error messages about missing functions and the like.

I downloaded the latest version of PHP4, recompiled it and thought the problem was solved, but it stuck around. Later I found out that the directory the new modphp4 file was placed in was not where Apache expected it to be. As soon as I moved it into the right place it has been happily ever after.

Do you know your PHP was compiled with XML support?

2:47 pm on Mar 15, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 24, 2005
posts:61
votes: 0


Terry,

Contact your host/server admin. Apache (assuming that's your web server) needs to be explicitly configured to allow the "AddType" directive in .htaccess files.

Specifically, you'll need the AllowOverride directive configured with a directive-type of "FileInfo" or "All".

You can read more in the apache documentation, at the following URL

[httpd.apache.org...]