isa
isa

Reputation: 462

PHP -> XML output is different on IE

I have been trying to output XML with PHP but encountered a strange(!) error in Internet Explorer.

The expected xml output is this:(simplified)

<root>
<match_id>12</match_id>
<stadium_id>43</stadium_id>
<tribune_id>2</tribune_id>
<fan_id>453</fan_id>
</root>

I am producing this output with the following PHP code:

echo "<?xml version='1.0' encoding='utf-8' ?>
<root>
<match_id>"; echo $match->getId(); echo "</match_id>
<stadium_id>43</stadium_id>
<tribune_id>2</tribune_id>
<fan_id>".$_SESSION['user_id']."</fan_id>
</root>";

In Firefox, the output is same as expected. However, in IE, the output is this:

<?xml version="1.0" encoding="utf-8" ?> 
<root>
  <match_id>0</match_id> 
  <stadium_id>43</stadium_id> 
  <tribune_id>2</tribune_id> 
  <fan_id /> 
</root>

This is a really annoying error. I have set the PHP header for XML output, and changed lots of other things but could not make it work.

The $match->getId() part is just returning an integer but IE always shows this value as 0. If I set <fan_id> and <match_id> manually, IE shows the values correctly.

By the way, I am using this XML output in Flash (AS3) and this also shows the same result with IE.

What am I doing wrong?

Upvotes: 1

Views: 308

Answers (3)

Soliman
Soliman

Reputation: 441

Try:

  header( "content-type: application/xml; charset=ISO-8859-15" );   

OR

$doc = new DOMDocument;

$root = $doc->createElement('root');  
$doc->appendChild($root);

$match_id = $doc->createElement('match_id', $match->getId());
$root->appendChild($match_id);

$stadium_id = $doc->createElement('stadium_id', '43');
$root->appendChild($stadium_id);

$tribune_id = $doc->createElement('tribune_id', '2');
$root->appendChild($tribune_id, '2');

$fan_id = $doc->createElement('fan_id', $_SESSION['user_id']);
$root->appendChild($fan_id);

echo $doc->saveXML();

//$doc->save('file.xml'); // if you want to write to file 

Upvotes: 1

leepowers
leepowers

Reputation: 38318

This looks like it's due to a session difference - the IE session isn't storing the user id. Similarly, $match->getId() is actually 0 - I imagine you'd get a similar result using Chrome or Safari or a web browser on any other computer.

One other thing: Flash may not be sending the PHP session cookie to the server on the request - which would match the IE behavior / no valid session.

Upvotes: 1

Mchl
Mchl

Reputation: 62387

What browser is used has no effect on how your PHP executes (because it's executed on server, not in browser). Notice that in the second example your fan_id is also empty, which indicates something's wrong with your session setup. Investigate this.

Having said all that: did you consider using simplexml() to output XML from PHP? It's much more fun to use than echoing tags.

Upvotes: 0

Related Questions