The GET request method is used to fetch data from the server. We cannot send data in the body of an HTTP GET message, but we can still send some data to the server in URL parameters. In this HTTP GET request example, we are downloading the content of the ReqBin echo URL. The Accept: */* request header tells the server that the client accepts all media types. The Content-Type: text/html response header informs the client that the server returned HTML for this HTTP GET request. The PHP code was automatically generated for the GET Request example.
The Hypertext Transfer Protocol (HTTP) is the core of the World Wide Web and powers websites and mobile applications. The HTTP is designed to transfer information between networked devices and provides a framework for client/server communication. HTTP works as a request/response protocol between client and server. For example, a browser (client) sends an HTTP GET request to a web server (server); the server then returns the response to the browser. The HTTP request contains the HTTP method (mostly GET), target URL, HTTP request headers, and additional URL parameters. The HTTP response includes information on the request's status (mostly 200), the requested content (HTML, JSON, image, etc.), and optional HTTP headers on how to interpret this content. All modern programming languages natively support HTTP, including PHP.
What is HTTP GET request?
HTTP GET request method is used to retrieve data from a specified URL. The GET is the most popular HTTP request method. GET requests should only receive data and should not affect the state of the server.
HTTP GET Request Format
The GET request consists of the request-line and HTTP headers section. The GET request-line begins with an HTTP method token, followed by the request URI and the protocol version, ending with CRLF. Space characters separate the elements. Below is an example of a GET request to the ReqBin echo server and the auto-generated PHP code.
HTTP GET Request Example
GET /echo HTTP/1.1
Some notes on HTTP GET Requests
GET request method is used to get a resource from the server
GET requests cannot have a message body, but you still can send data to the server using the URL parameters
GET requests should only receive data. If you want to change data on the server, use POST, PUT, PATCH, or DELETE methods
GET method is defined as idempotent, which means that multiple identical GET requests should have the same effect as a single request
HTTP GET Request Examples
Example of getting HTML page from ReqBin echo URL with PHP.
GET HTML Request Example
GET /echo HTTP/1.1
Example of getting JSON data from ReqBin echo URL with PHP.
GET JSON Request Example
GET /echo/get/json HTTP/1.1
Example of getting XML data from ReqBin echo URL with PHP.
GET XML Request Example
GET /echo/get/xml HTTP/1.1