PHP XML header
PHP XML header is the XML content type output from PHP file instead of default header content of text/html. Setting the header content type to XML indicates that the output to the browser is in XML format. XML stands for Extensible Markup Language, which denotes rules for document encoding. PHP being an open-source programming language, is used to dynamically set web content and applications at the server end. Similar to how Dynamic HTML content is generated in PHP, dynamic XML content can also be generated in PHP.
Web development, programming languages, Software testing & others
Syntax
Given below is the syntax of the PHP XML header:
Above is the basic syntax of the Content-type to be changed to XML when using PHP XML header.
header(header, replace, http_response_code)
The basic Header function sends raw HTTP header to the client; it is called before the output is sent.
- header: It specified the header string to be sent and is a mandatory parameter.
- replace: It indicates if the header should replace the previous header or add a new header of the same type. It is an optional parameter, and the default being TRUE, i.e. will replace. Boolean FALSE allows multiple headers with the same type.
- http_response_code: It forces HTTP response code to specific value and is an optional parameter.
In PHP, no output can occur before the occurrence of the header() sent to the browser. Content-Type of header tells the browser on how to handle the content. For e.g., If there is a PDF file which the browser can’t support, it will give an option to use other application. It will tell to use other application on our system or will ask to download a particular application.
If the Content-type is text/xml, no charset is specified as the file is treated as us-ascii. For different charset, it needs to specified in the header. For XML use in PHP, the Content-type is specified as ‘application/xml’ and charset to be ‘utf-8’. Content-type Header is part of the HTTP protocol and tells the client or server what type of data to expect and how to interpret it.
Examples of PHP XML header
Different examples are mentioned below:
Example #1
"; echo ""; echo "Hello, this is first PHP XML header"; echo "\n"; echo "Run the code to see the output "; echo " "; ?>
Here the content type is header is now in XML format.
XML is supported by a wide range of web users and XML tools which are device neutral, vendor-neutral and platform-neutral.
Content-type: text/xml; charset="utf-8"
Above is the recommended charset value to be used with text/xml. MIME and xml processors consider the entity as UTF-8 enclosed. If in cases where data is sent using SMTP request, xml has to use content transfer encoding or base64 encoding. For other types of requests like ESMTP, NNTP, HTTP, etc., the content transfer encoding is not required.
Content-type: text/xml; charset=»utf-16″
utf-16 charset is used only when the xml is transmitted through HTTP with a binary clean protocol without performing any CR and LF transformations. As HTTP is a binary protocol, the content transfer encoding is not necessary here.
Here charset parameter has been omitted. In such cases, xml processors hence assume the charset to be ‘us-ascii’. This default holds true for text/xml is transported using an HTTP request.
But, omission of charset is not at all recommended for text/xml header.
Content-type: application/xml; charset=»utf-16″
It is recommended to be used with header content type as application/xml. As the charset is provided here, xml processors treat the entity as utf-16 encoded.
Content-type: application/xml
Here, there is neither the BOM parameter nor the charset parameter. XML, which is unaware of the processor, should not make any assumptions about the XML entity’s charset.
Example #2
"Karthik", "empid" => 675, "role" => "UI developer", "age" => 25, "city" => "Vizag" ]; echo json_encode($arr); ?>
Example #3
"; $data = [1,2,3,4,5,6,7,8,9]; echo json_encode($data); ?>
Conclusion
With this, we conclude our topic ‘PHP XML header’. We have seen what PHP XML header is; changing the content type to XML brings out a lot of difference for the http request-response. We have analyzed the syntax for declaring the xml header in PHP code. A charset is one of the important parts of PHP which describes the form of data sent. I saw various charsets such as utf-8 and utf-16, default us-ascii charsets. Also saw how the php xml header works with some of the examples listed above.
Recommended Articles
This is a guide to the PHP XML header. Here we discuss the introduction to PHP XML header along with examples for better understanding. You may also have a look at the following articles to learn more –
25+ Hours of HD Videos
5 Courses
6 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
92+ Hours of HD Videos
22 Courses
2 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
83+ Hours of HD Videos
16 Courses
1 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
PHP Course Bundle — 8 Courses in 1 | 3 Mock Tests
43+ Hours of HD Videos
8 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5