PHP XML header

WBOY
Release: 2024-08-29 13:00:52
Original
686 people have browsed it

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.

ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax

Given below is the syntax of the PHP XML header:

<?php header('Content-Type: text/xml'); ?>
Copy after login

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)
Copy after login

The basic Header function sends raw HTTP header to the client; it is called before the output is sent.

Arguments:

  • 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.
<?php
header ("Content-Type:text/xml");
?>
<xml_content>
…………………………
</xml_content>
Copy after login

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

Code:

<!DOCTYPE html>
<html>
<body>
<?php
header("Content-type: text/xml");
echo "<?xml version='1.0' encoding='UTF-8'?>";
echo "<note>";
echo "Hello, this is first PHP XML header";
echo "\n";
echo "<message>Run the code to see the output</message>";
echo "</note>";
?>
</body>
</html>
Copy after login

Output:

PHP XML header

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"
<?xml version="1.0" encoding="utf-8"?>
Copy after login

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"
{BOM}<?xml version='1.0' encoding='utf-16'?>
Copy after login

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.

Content-type: text/xml
{BOM}<?xml version="1.0" encoding="utf-16"?>
Copy after login

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"
{BOM}<?xml version="1.0"?>
Copy after login

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
<?xml version='1.0'?>
Copy after login

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

Code:

<!DOCTYPE html>
<html>
<body>
<?php
header("Content-type: application/xml");
$arr =[
"empName" => "Karthik",
"empid" => 675,
"role" => "UI developer",
"age" => 25,
"city" => "Vizag"
];
echo json_encode($arr);
?>
</body>
</html>
Copy after login

Output:

PHP XML header

Example #3

Code:

<!DOCTYPE html>
<html>
<body>
<?php
header("Content-type: text/xml");
echo "<?xml version='1.0' encoding='UTF-8'?>";
$data = [1,2,3,4,5,6,7,8,9];
echo json_encode($data);
?>
</body>
</html>
Copy after login

Output:

PHP XML header

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.

The above is the detailed content of PHP XML header. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!