cURL overview

 

Overview

This article explains how to use the command-line curl tool at DreamHost.

Background

cURL is free software that allows you to transfer between servers. The cURL project offers two sub-projects:

  • curl — This is the command line tool you can run on your local computer or your DreamHost server. It offers several command options for you to send and receive data. 'curl' uses 'libcurl'.
  • libcurl — This is a library that supports many different protocols. It's very common for the PHP programming language to use this library. See the cURL manual for further details.

Prerequisites

This article assumes you have created a shell user and are able to log into your server via SSH.

Running curl commands

The following sections provide common commands you can use with your website.

View the curl version installed

Run the following to display the version of curl your shell is using.

[server]$ curl --version

View the code of a specific webpage

This returns the source-coding of a specific page.

[server]$ curl https://example.com

Save the code of a specific webpage in a new file

To save source-code to a new file, use the -o flag. This uses the lowercase 'o' character. Make sure to change example.com-CODE to the new file you're saving to.

[server]$ curl -o example.com-CODE http://example.com 

Save to a file of the same name

If you do not want to save to a new file name, you can save to a file that's the same as the page you're accessing. For example, if you're accessing example.com/index.html, this will create a new file titled index.html with the saved code. Use the -O flag with a capital 'O' character.

The syntax would be curl -O <website-url>

[server]$ curl -O https://example.com/index.html 

Test a website connection

Use the -v flag to view connection data, then the full code of the page you entered.

[server]$ curl -v https://wordpress.org/
* About to connect() to wordpress.org port 443 (#0)
*   Trying 66.155.40.250... connected
* Connected to wordpress.org (66.155.40.250) port 443 (#0)
* successfully set certificate verify locations:

The above shows a successful connection. If it failed, you'd see the failure in the response instead.

Check website headers

Use the -I flag to check website headers.

[server]$ curl -I http://example.com/index.php
  HTTP/1.1 200 OK
Date: Mon, 01 March 2024 20:55:09 GMT
Server: Apache
Last-Modified: Sun, 24 Sep 2023 22:45:30 GMT
ETag: "2f3-5387d8a979b59"
Accept-Ranges: bytes
Content-Length: 755
Vary: Accept-Encoding
Content-Type: text/html

If you want a specific header not shown, you must manually specify it using the -H flag. For example, run the following to check if gzip is enabled:

[server]$ curl -I -H 'Accept-Encoding: gzip,deflate' http://example.com/index.php
  HTTP/1.1 200 OK
Date: Mon, 01 March 2024 20:55:09 GMT
Server: Apache
Last-Modified: Sun, 24 Sep 2023 22:45:30 GMT
ETag: "2f3-5387d8a979b59"
Accept-Ranges: bytes
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 428
Content-Type: text/html

If gzip is enabled, you'll see this line in the output:

Content-Encoding: gzip

Troubleshooting

Moved Permanently

It's possible that when you run curl on a website URL, you'll see a Moved Permanently response. For example:

[server]$ curl http://www.example.com

<html>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://example.com">here</a>.</p>
</body></html>

This usually just means the URL does (or doesn't) use the 'www' subdomain before it. Try again with (or without) www.

Otherwise, you can use the -L flag to redirect you automatically.

[server]$ curl -L http://www.example.com

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?