Monday, July 12, 2010

PHP posing as XML

Want to set up your own custom RSS feeds in PHP? Need to pass a dynamic XML document to another service? It's easier than you think! Disguise your PHP file as XML with the following headers:

header("Pragma: public"); header("Content-Type: application/xml; charset=utf-8");

The output of your PHP document should be valid XML. The headers will convince whatever's reading it that it's an XML file.

Your file can still use the .php extension (and will need to, unless you set up some special server-side handling), but you can refer to it as if it were an XML document. It works in RSS readers, etc. just as if it were an XML file, but you can generate the content dynamically and change it depending on the GET parameters and so forth.

Presumably, you can use these headers in a language that isn't PHP to achieve the same effect.

No comments:

Post a Comment