generate a dynamic XML response from a PHP script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
itp
Forum Commoner
Posts: 67
Joined: Fri Jun 15, 2007 6:50 am

generate a dynamic XML response from a PHP script

Post by itp »

I am trying to generate a dynamic XML response from a PHP script. Based on what I have learned about the process so far, it is my understanding that the following segment of code should generate a XML response and will appear as an XML tree in when viewed in Internet Explorer showing XML tags and content.

However what I get looks like a regular HTML page.

If I save the document to a file with .xml extention it is viewed as a tree in IE, however when generated dynamically it looks like a regular html document.

I hope I am expressing myself clearly. There are alot of new concepts here for me. 8O


Here is a simplified example.

Code: Select all

<?php 
header('Content-Type: text/xml; charset=UTF-8'); 
header('Content-Disposition: inline; filename="test.xml"'); 
echo "<?xml version='1.0' encoding='UTF-8'?>";
echo <<<BODY
<invoice>
<to>12345</to> 
<from>TP937373</from> 
<heading>Invoice Header</heading> 
<body>Invoice Detail</body> 
</invoice>
BODY;
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: generate a dynamic XML response from a PHP script

Post by Christopher »

You might want to try:

Code: Select all

<?php 
header('Content-Type: text/xml; charset=UTF-8'); 
header('Content-Disposition: inline; filename="test.xml"'); 
echo "<?xml version='1.0' encoding='UTF-8'?>";
echo <<<BODY
<?xml version="1.0"?>
<invoice>
<to>12345</to> 
<from>TP937373</from> 
<heading>Invoice Header</heading> 
<body>Invoice Detail</body> 
</invoice>
BODY;
?>
(#10850)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
header('Content-Type: text/xml; charset=UTF-8');
header('Content-Disposition: inline; filename="test.xml"'); 

echo '<?xml version="1.0" encoding="UTF-8"?>
<invoice>
 <to>12345</to>
 <from>TP937373</from>
 <heading>Invoice Header</heading>
 <body>Invoice Detail</body>
</invoice>';
?>
works fine for me. Firefox even honors the filename="test.xml" directive, IE doesn't
Post Reply