16

I'm using the header() function to turn a file into XML standard. The problem is when I use <?php header("Content-type: text/xml; charset=utf-8"); ?> it just renders the <?xml version="1.0"?>, without the enconding/charset. Am I using it wrongly?

3 Answers 3

14

The header() function just modifies HTTP headers. The code you posted sets a Content-Type header, which is important for telling browsers and other clients what type of file you're serving.

The <?xml version="1.0"?> line you're talking about is part of the document itself, and not affected by the HTTP headers.

Your tags say you're using DOM to create your XML document. If you change your DomDocument constructor to also pass the charset,

$doc = new DomDocument('1.0', 'UTF-8');

It should output that charset in the XML document.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you BOTH. I was kind of lazy to search for it. I've thought of something like that, but I was too lazy to even think. :o)
2

header just sets a HTTP header in the result. PHP doesn't do anything else with the value, so it's up to you to make sure it's being used properly.

If you're using an XML library to generate your XML (including the prologue), check the documentation for that library. If you're outputting the XML "by hand" (o, you need to add the necessary attribute to the prologue yourself.

1 Comment

Thank you. I was kind of lazy to search for it. I've thought of something like that, but I was too lazy to even think. :o)
0

You can also use

<?php echo '<?xml version"1.0" encoding="UTF-8"?>'; ?>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.