Sending GET Request with Curl [PHP Code]

To make a GET request using Curl, run the curl command followed by the target URL. Curl automatically selects the HTTP GET request method unless you use the -X, --request, or -d command-line option. The target URL is passed as the first command-line option. To add additional HTTP headers, use the -H command line option. Curl automatically adds an Accept: */* request header if no additional headers are passed, which tells the server that the Curl client can accept data in any format. In this Curl GET example, we send a Curl request to the ReqBin echo URL. Click Run to execute the Curl GET Request example online and see the results. The PHP code was automatically generated for the Curl GET Request example.
Sending GET Request with Curl [PHP Code] Run
curl https://reqbin.com/echo
Updated: Viewed: 133166 times
PHP code for Curl GET Request example

PHP code for Curl GET Request Example

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

What is Curl?

Curl stands for Client for URLs, and it is a popular command-line tool for Linux, Windows, and macOS for transferring data over the network using HTTP, HTTPS, FTP, and SFTP protocols. You can make GET, POST, and HEAD requests to the server, retrieve HTTP headers, download HTML pages, upload files, submit forms, and more.

What is HTTP GET request?

The HTTP GET method requests a resource from the server using the provided URL. The GET method is one of nine standard HTTP (Hypertext Transfer Protocol) methods. The primary purpose of the GET method is to retrieve data from the server. HTTP GET requests cannot send data to the server in the body of a GET message or change the server's state. But you can still pass data to the server in URL parameters.

Curl GET Request Examples

The following are examples of sending a GET request to Curl:

Basic Curl GET request example

Curl is effortless to use, and this basic Curl example demonstrates how easy it is to make a GET request to the target server using Curl.

Basic Curl GET request example
curl https://reqbin.com/echo

The server's response to our Curl request:

Server response to Curl request
HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 643
      
[html code here]

Sending HTTP headers with a Curl GET request

To make a GET request with HTTP headers, use the -H command-line option. You can pass as many HTTP headers with your Curl GET request as you like using the -H command line parameter multiple times.

Curl GET Request Example with custom HTTP headers
curl https://reqbin.com/echo
   -H "Cache-Control: must-revalidate"
   -H "Pragma: no-cache"
   -H "Expires: 0"

Getting only HTTP headers using Curl

To fetch only HTTP headers, use the -I command-line option. In this case, Curl will use the HTTP HEAD method instead of the HTTP GET request method and will not download the body of the HTTP response message.

Curl GET HTTP headers example
curl -I https://reqbin.com/echo

Getting JSON using Curl

To receive data in JSON format with Curl, you must pass the "Accept: application/json" HTTP header to the server. If you do not pass this header, the server may automatically choose your client's most appropriate data type and return the data in a different format. The following is an example of getting JSON from a ReqBin echo URL:

Curl GET JSON example
curl https://reqbin.com/echo/get/json
   -H "Accept: application/json"

Checking if the target URL supports HTTP/2 using Curl

To check if the target URL supports HTTP/2 using Curl, you can send a Curl HEAD request along with the --http2 command line parameter.

Curl HTTP/2 support check
curl -I --http2 https://reqbin.com/echo

In the response, you will see the HTTP/2 200 status line if your server supports the HTTP/2 protocol or HTTP/1.1 200 otherwise.

Sending cookies along with a GET request using Curl

You can send cookies to the server using the -b command-line option followed by a string with the cookie or the name of the file containing the cookies.

Curl GET Request Example with Cookies
curl https://reqbin.com/echo
   -b "session=eJwlzj0wMQG7eO4Q"

Getting a specific range of bytes from a resource using Curl

To get a specific range of resource bytes from a target URL using Curl, you can use the -r command line option.

Curl example to get a specific range of bytes
curl https://reqbin.com/echo
   -r 0-15000

Limiting the maximum transfer rate for Curl GET requests

You can use the-- limit-rate command line option to limit the maximum transfer rate for uploading and downloading files in Curl. By default, the speed is measured in bytes per second, but you can specify the speed in kilobytes (K), megabytes (M), or gigabytes (G) using a suffix.

Curl transfer rate limit example
curl https://reqbin.com/echo
   --limit-rate 50K

How to tell Curl to follow redirects?

By default, Curl doesn't follow 300x redirects. You can force Curl to follow the redirects given in the Location header using the -L command-line option.

Curl GET Request Example with Follow Redirects option
curl -L https://reqbin.com/echo

See also

Generate code snippets for PHP and other programming languages

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