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.
The server's response to our Curl request:
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.
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.
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:
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.
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.
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.
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.
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.