Getting XML from the Server [PHP Code]

To retrieve XML from the server using PHP, you need to send a GET request and specify the "Accept: application/xml" HTTP header in the request. The Accept header tells the server that the client is expecting XML data. Without this header, the server may return data in a different format. The Content-Type: application/xml response header informs the client that the server returned XML. In this PHP GET XML Example, we send a GET request to the ReqBin echo URL with Accept: application/xml request header. Click Send to execute PHP GET XML Request example online and see the results. The PHP code was automatically generated for the GET XML example.
Getting XML from the Server [PHP Code] Send
GET /echo/get/xml HTTP/1.1
Host: reqbin.com
Accept: application/xml

Updated: Viewed: 47136 times
PHP code for GET XML example

PHP code for GET XML Example

This PHP code snippet was generated automatically for the GET XML example.
<< Back to the GET XML example

What is XML?

XML (Extensible Markup Language) is an extensible markup language that provides structured information: data, documents, configuration, and more. XML is called extensible because it doesn't fix the markup used in documents: you can create markup according to the needs of a particular area, limited only by the rules of XML syntax. An XML file is a simple text file that uses XML tags to describe the structure of a document, how it is stored and transported, and the data itself.

XML Example
<Request>
  <Login>
     login
  </Login>
  <Password>
     password
  </Password>
</Request>

What is HTTP GET request?

HTTP GET is the most popular of the nine commonly used HTTP methods. The GET request method is used to retrieve data from the specified URL, cannot contain data in the body of the GET request, and should not change the server state. The GET method is defined as idempotent, which means that several similar GET requests should have the same effect on the server as a single request.

HTTP GET Request Example
GET /echo/ HTTP/1.1
Host: reqbin.com

Get XML Example

An example of getting to our XML GET request:

Get XML Request Example
GET /echo/get/xml HTTP/1.1
Host: reqbin.com
Accept: application/xml

An example of a server response to our XML GET request:

Server XML Response Example
<?xml version="1.0" encoding="utf-8"?>
<Response>
    <ResponseCode>0</ResponseCode>
    <ResponseMessage>Success</ResponseMessage>
</Response>

Why is it important to send the Accept header in the XML request?

The "Accept: application/xml" header indicates that the client expects to see XML in the server's response. Without this header, the server can return data in a different format.

Accept Header Example
Accept: application/xml

When the server returns XML in its response, it notifies the client using the "Content-Type: application/xml" response header.

Content-Type Response Header Example
Content-Type: application/xml

How to view XML directly in the browser?

Most browsers can display XML documents right in the browser. To do this, enter the address of the XML document in the address bar, and the browser will display the XML in a convenient tree format.

How to post XML to the server?

To post XML to the server, you need to make a POST request, include the XML in the request body and specify the correct MIME type. The MIME type for XML is "application/xml". The Accept: application/xml request header tells the server that the client is expecting XML, and the "Content-Type: application/xml" response header indicates that the server returned XML. Below is an example of sending XML to the ReqBin echo URL:

POST XML Request Example
POST /echo/post/xml HTTP/1.1
Host: reqbin.com
Content-Type: application/xml
Accept: application/xml
      
<?xml version="1.0" encoding="utf-8"?>
<User>
    <name>name</name>
    <rating>rating</rating>
</User>

See also

Generate code snippets for PHP and other programming languages

Convert your GET XML request to the PHP, JavaScript/AJAX, Node.js, Curl/Bash, Python, Java, C#/.NET code snippets using the PHP code generator.