Sending HTTP GET Request [PHP Code]

The GET request method is used to fetch data from the server. A GET request should only fetch data from the server and cannot include data in the GET message body, but you can still send some data to the server in URL parameters. In this PHP 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. Click Send to run the PHP GET Request Example online and see the results. The PHP code was automatically generated for the GET Request example.
Sending HTTP GET Request [PHP Code] Send
GET /echo HTTP/1.1
Host: reqbin.com
Accept: */*

Updated: Viewed: 79029 times
PHP code for GET Request example

PHP code for GET Request Example

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

What is HTTP?

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
Host: reqbin.com
Accept: */*

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
Host: reqbin.com
Accept: text/html

Example of getting JSON data from ReqBin echo URL with PHP.

GET JSON Request Example
GET /echo/get/json HTTP/1.1
Host: reqbin.com
Accept: application/json

Example of getting XML data from ReqBin echo URL with PHP.

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

See also

Generate code snippets for PHP and other programming languages

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