Top 12 Curl Commands with Examples [PHP Code]

Curl is a popular command-line utility for transferring data to or from a server using over 25+ protocols. The Curl command-line tool provides several advanced options such as user authentication, proxy support, resuming transmission, limiting bandwidth and transfer rates, and more. Curl commands work without user interaction and are therefore ideal for use in automation scenarios. This article will go over the 12 most essential Curl commands for day-to-day use for making requests over HTTP/HTTPS protocols. The PHP code was automatically generated for the Curl Commands example.
Top 12 Curl Commands with Examples [PHP Code] Run
curl https://reqbin.com/echo
Updated: Viewed: 40262 times
PHP code for Curl Commands example

PHP code for Curl Commands Example

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

How to install Curl?

You can install Curl on Linux, Windows, and macOS in the following ways:

Install Curl on Linux

On Linux, enter the following command to install Curl:

Install Curl on Linux Example
$ sudo apt install curl
OR
$ sudo apt-get install curl

Install Curl on Windows

To use Curl on Windows, download the installer from the Curl official website and unpack the curl.zip archive to the desired local folder on your computer. Add the Curl folder (usually C:\Curl\bin) to your Windows PATH environment variable to invoke the Curl command from anywhere else. Enter curl --version on the command line to make sure you can use Curl on Windows.

Check Curl version
curl --version

Install Curl on macOS

Curl comes with macOS already. You can update Curl to the latest version by installing macOS Homebrew Software Package Manager. After installing Homebrew, open a Terminal and type:

Install Curl on macOS Example
brew install curl

How to use the Curl command-line tool?

The Curl command syntax is protocol-dependent. For the HTTP protocol, the Curl command line syntax is as follows:

Curl syntax
curl [options] [URL]

Below is a list of the 12 best Curl commands that you will use the most during your work.

1. Get resource content by URL

Curl command to get the content of a page
curl https://reqbin.com/echo

If no parameters are specified, Curl sends an HTTP GET request to the server and outputs the resource's content (for example, the HTML code of a page) to standard output (usually a terminal window).

2. Save URL content to a file

Curl command to save URL content to a file
curl -o logo.png https://reqbin.com/static/img/logo.png

The Curl -o command-line option tells Curl to save the contents of the URL in the current working directory with the specified file name. The resource is downloaded with the GET request method.

3. Download multiple files at once

Curl command to download multiple files
curl -O https://reqbin.com/static/img/code/curl.png 
   -O https://reqbin.com/static/img/code/java.png 
   -O https://reqbin.com/static/img/code/python.png

The list of URLs is passed to Curl with one of the -o or -O command-line options. As we saw in example #2, for the command line parameter -o, you can pass the file name under which the resource will be saved, and the -O command line parameter tells Curl to save the downloaded resource with the original file name.

5. Check Page HTTP headers

Curl command to check page HTTP headers
curl -I https://reqbin.com/echo

The -I parameter tells Curl to send an HTTP HEAD request to the server instead of a GET. The HEAD request is similar to a GET, except that the server only returns HTTP headers. This is useful if you only want to check the URL headers and not load the page content (to save Internet traffic).

6. Force Curl to use HTTP/2 protocol

Curl command to use HTTP/2 protocol
curl --http2 https://reqbin.com

The --http2 option forces Curl to use the HTTP/2 protocol instead of HTTP/1.1. Combined with the -I command line parameter, we can use this Curl command to check if a website supports HTTP/2.

7. Do Follow Redirects

Curl command to follow redirects
curl -L http://www.reqbin.com/echo

By default, Curl does not follow redirects (HTTP status codes 301 and 302). The -L option tells Curl to do HTTP redirects.

8. Use proxy server

Curl command to connect via a proxy server
curl -x proxy.domain.com:8080 -U user:password https://reqbin.com

If you are behind a proxy server, the -x command-line option will allow you to send requests through the proxy server. The -U command-line option passes the proxy username and password to the proxy.

9. Provide additional HTTP headers with request

Curl command to send additional HTTP headers to server
curl -H "Accept: application/json" https://reqbin.com/echo/get/json

The -H command-line option allows additional HTTP headers to be passed to the server. You can provide as many HTTP headers as you want using the -H option multiple times.

10. Send data to the server

Curl command to sending data to the server
curl -d '{"id": 123456}'
   -H "Content-Type: application/json"
   https://reqbin.com/echo/post/json

The -d command line parameter tells Curl to send the supplied data to the server using the HTTP POST request method. The -d option is usually followed by the -H parameter to indicate the data type.

11. Change the User-Agent string

Curl command to change the User-Agent string
curl --user-agent "MyAppName 1.0" https://reqbin.com/echo

The --user-agent command-line option allows you to pass any string to be used instead of the standard Curl User-Agent string. This can be useful if the server only expects requests from certain browsers.

12. Send Cookies to Website

Curl command to send cookies with the request
curl -b "name1=value1; name2=value2" https://reqbin.com

The -b command line parameter allows you to send cookies back to the website. You can use the -b switch with the file's name that contains the cookies or pass the cookies in the string.

See also

Generate code snippets for PHP and other programming languages

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