Как скачивать файлы в linux с помощью curl

Testing protocol support with cURL

Due to the wide range of protocols supported by cURL, you can even use it to test protocol support. If you want to check if a site supports a certain version of SSL, you can use the  or  flags. For example, if you want to check if a site supports TLS v1.2, you can use:

curl -v --tlsv1.2 https://www.booleanworld.com/

The request takes place normally, which means that the site supports TLSv1.2. Now, let us check if the site supports SSL v3:

curl -v --sslv3 https://www.booleanworld.com/

This command throws a  error, because the server doesn’t support this version of SSL.

Please note that, depending on your system and the library version/configuration, some of these version options may not work. The above output was taken from Ubuntu 16.04’s cURL. However, if you try this with cURL in MacOS 10.14, it gives an error:

You can also test for HTTP protocol versions in the same way, by using the flags ,  or .

User Agent

Very similar to the referer field, all HTTP requests may set the User-Agent field. It names what user agent (client) that is being used. Many applications use this information to decide how to display pages. Silly web programmers try to make different pages for users of different browsers to make them look the best possible for their particular browsers. They usually also do different kinds of javascript, vbscript etc.

At times, you will see that getting a page with curl will not return the same page that you see when getting the page with your browser. Then you know it is time to set the User Agent field to fool the server into thinking you’re one of those browsers.

To make curl look like Internet Explorer 5 on a Windows 2000 box:

Or why not look like you’re using Netscape 4.73 on an old Linux box:

MingW32

Make sure that MinGW32’s bin dir is in the search path, for example:

then run in the root dir. There are other make targets available to build libcurl with more features, use:

  • to build with Zlib support;
  • to build with SSL and Zlib enabled;
  • to build with SSH2, SSL, Zlib;
  • to build with SSH2, SSL, Zlib and SSPI support.

If you have any problems linking libraries or finding header files, be sure to verify that the provided files use the proper paths, and adjust as necessary. It is also possible to override these paths with environment variables, for example:

It is also possible to build with other LDAP SDKs than MS LDAP; currently it is possible to build with native Win32 OpenLDAP, or with the Novell CLDAP SDK. If you want to use these you need to set these vars:

or for using the Novell SDK:

If you want to enable LDAPS support then set LDAPS=1.

curl сканер

Security Task Manager показывает все запущенные сервисы Windows, включая внедренные скрытые приложения (например, мониторинг клавиатуры или браузера, авто вход). Уникальный рейтинг надежности указывает на вероятность того, что процесс потенциально может быть вредоносной программой-шпионом, кейлоггером или трояном.

Бесплатный aнтивирус находит и удаляет неактивные программы-шпионы, рекламу, трояны, кейлоггеры, вредоносные и следящие программы с вашего жесткого диска. Идеальное дополнение к Security Task Manager.

Reimage бесплатное сканирование, очистка, восстановление и оптимизация вашей системы.

Другие процессы

installer.exe ikeymain.exe aswregsvr.exe curl.exe rpmdaemon.exe 14bar.dll ccevtmgr.exe windscribe.exe msert.exe macro_hookkey.dll nierserver.exe

Forms explained

Forms are the general way a website can present a HTML page with fields for the user to enter data in, and then press some kind of ‘OK’ or ‘Submit’ button to get that data sent to the server. The server then typically uses the posted data to decide how to act. Like using the entered words to search in a database, or to add the info in a bug tracking system, display the entered address on a map or using the info as a login-prompt verifying that the user is allowed to see what it is about to see.

Of course there has to be some kind of program on the server end to receive the data you send. You cannot just invent something out of the air.

3: Поддержка перенаправлений

Пока что все примеры использовали полностью определенные URL-адреса, которые включают протокол https://. Если бы вы попытались получить файл robots.txt и указали бы только домен, вы не увидели бы никаких результатов, потому что сайт может перенаправлять запросы с http:// на https://.

Вы можете проверить это с помощью флага -I, который отображает заголовки запроса, а не содержимое файла:

Выходные данные показывают, что URL-адрес был перенаправлен. Первая строка вывода сообщает, что он был перемещен, а строка Location сообщает, куда именно:

Вы можете использовать curl и отправить другой запрос вручную. Также вы можете использовать аргумент –location (или –L), который позволит утилите curl отправить запрос в новое место, если она встречает перенаправление. Попробуйте сделать это:

На этот раз вы увидите ожидаемый результат, поскольку curl поддерживает перенаправление:

Вы можете комбинировать аргумент -L с некоторыми из вышеупомянутых аргументов, чтобы загрузить файл в вашу локальную систему:

Важно! Многие онлайн-ресурсы предлагают использовать curl для загрузки скриптов и их выполнения. Однако прежде чем делать исполняемыми и запускать какие-либо загруженные скрипты, вы должны проверить их содержимое

Используйте команду less, чтобы проверить код и убедиться, что он не причинит вреда вашей системе.

👨‍💻 Опрос

Проверьте свою внимательность. Что означают следующие параметры?

Tip: Использование curl в Терминале или iTerm OS Mac обеспечивают намного более простую работу, чем использование командной строки в Windows. Если серьезно относиться к документации API, работая на ПК, стоит подумать о переходе с OS Windows. Будет много утилит, которые мы установим при помощи Терминала, который просто работает на Mac. Кроме того, находясь в Силиконовой долине, и используя ПК вместо Mac можно показаться старомодным для окружающих (см. Почему большинство стартапов покупают MacBook для своих сотрудников).

Для более подробного изучения curl в документировании REST API можно посмотреть REST-esting with curl.

Location header

When a resource is requested from a server, the reply from the server may include a hint about where the browser should go next to find this page, or a new page keeping newly generated output. The header that tells the browser to redirect is .

Curl does not follow headers by default, but will simply display such pages in the same manner it displays all HTTP replies. It does however feature an option that will make it attempt to follow the pointers.

To tell curl to follow a Location:

If you use curl to POST to a site that immediately redirects you to another page, you can safely use () and / together. curl will only use POST in the first request, and then revert to GET in the following operations.

Simple Usage

Get the main page from a web-server:

Get the README file the user’s home directory at funet’s ftp-server:

Get a web page from a server using port 8000:

Get a directory listing of an FTP site:

Get the definition of curl from a dictionary:

Fetch two documents at once:

Get a file off an FTPS server:

or use the more appropriate FTPS way to get the same file:

Get a file from an SSH server using SFTP:

Get a file from an SSH server using SCP using a private key (not password-protected) to authenticate:

Get a file from an SSH server using SCP using a private key (password-protected) to authenticate:

Get the main page from an IPv6 web server:

Get a file from an SMB server:

HTTPS is HTTP secure

There are a few ways to do secure HTTP transfers. By far the most common protocol for doing this is what is generally known as HTTPS, HTTP over SSL. SSL encrypts all the data that is sent and received over the network and thus makes it harder for attackers to spy on sensitive information.

SSL (or TLS as the latest version of the standard is called) offers a truckload of advanced features to allow all those encryptions and key infrastructure mechanisms encrypted HTTP requires.

Curl supports encrypted fetches when built to use a TLS library and it can be built to use one out of a fairly large set of libraries — will show which one your curl was built to use (if any!). To get a page from a HTTPS server, simply run curl like:

Запросы и ответы содержат заголовки

При вводе адреса веб-сайта мы видим только текст ответа. Но на самом деле происходит гораздо больше процессов. Когда мы делаем запрос, мы отправляем заголовок запроса, который содержит информацию о запросе. Ответ также содержит заголовок ответа.

Для того чтобы увидеть заголовок ответа в запрос curl, добавим ключ :

Заголовок будет включен над телом ответа:

Чтобы в ответе получить только заголовок, используем ключ

Заголовок содержит метаданные ответа. Вся эта информация передается в браузер при запросе URL в нашем браузере (например, при просмотре веб-страницы в Интернете), но браузер не отображает эту информацию. Можно просмотреть информацию заголовка с помощью консоли Chrome Developer Tools, перейдя на вкладку .

Теперь давайте уточним метод. Метод GET (чтение) подразумевается по умолчанию, когда не указан другой метод, но мы сделаем это здесь явно с параметром -X:

При посещении веб-сайта мы отправляем запрос, используя метод GET. Существуют и другие методы HTTP, которые можно использовать при взаимодействии с REST API. Вот общие методы, используемые при работе с конечными точками REST:

HTTP метод Описание
POST Создание ресурса
GET Чтение (получение) ресурса
PUT Обновление ресурса
DELETE Удаление ресурса

Note: Метод GET используется по умолчанию в запросах curl. При использовании curl для выполнения запросов HTTP, отличных от GET, необходимо указывать нужный метод HTTP.

Building libcurl.dll on Windows

Required tools

  • Microsoft Librarian (lib.exe)
  • Sample program app.d
import std.net.curl;
import std.stdio;

void main()
{
	writeln(get("https://google.com/"));
}

Building libcurl.dll 32 bit with SSL support

  1. Create directory C:\BUILD
  2. Save the following batch file as C:\BUILD\build-curl32.bat
  3. Run build-curl32 from Windows command prompt
@echo off

: 1) Path to MinGW\bin
: 2) Path to Digital Mars's implib.exe
SET PATH=C:\Dev\MinGW\bin;C:\Dev\dm\bin;
SET ZLIB_PATH=C:\BUILD\zlib-1.2.10
SET INSTALL_DIR=C:\D\dmd2\windows
: ---------------------------------------------------------------

: Delete object files from previous build
del /S zlib-1.2.10\*.o curl-7.52.1\*.o curl-7.52.1\*.res

cd zlib-1.2.10
mingw32-make -fwin32/Makefile.gcc
cd ..

cd curl-7.52.1
mingw32-make -C lib -f Makefile.m32 CFG=mingw32-winssl-zlib-ipv6 LDFLAGS=-static
strip -s lib\libcurl.dll

mkdir %INSTALL_DIR%\bin %INSTALL_DIR%\lib
copy lib\libcurl.dll %INSTALL_DIR%\bin
implib /system %INSTALL_DIR%\lib\curl.lib lib\libcurl.dll
cd ..

: Build sample
SET PATH=%INSTALL_DIR%\bin
dmd app.d -ofapp32.exe
app32

Building libcurl.dll 64 bit with SSL support

  1. Create directory C:\BUILD
  2. Save the following batch file as C:\BUILD\build-curl64.bat
  3. Run build-curl64 from Windows command prompt
@echo off

: 1) Path to MinGW-w64\bin
: 2) Path to Microsoft LIB.exe
: 3) Path to pexports.exe (http://sourceforge.net/projects/mingw/files/MinGW/Extension/pexports/)
SET PATH=C:\Dev\MinGW64\bin;C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\Bin\amd64;C:\Dev\MinGW\bin
SET ZLIB_PATH=C:\BUILD\zlib-1.2.10
SET INSTALL_DIR=C:\D\dmd2\windows
: ---------------------------------------------------------------

: Delete object files from previous build
del /S zlib-1.2.10\*.o curl-7.52.1\*.o curl-7.52.1\*.res

cd zlib-1.2.10
mingw32-make -fwin32/Makefile.gcc
cd ..

cd curl-7.52.1
mingw32-make -C lib -f Makefile.m32 CFG=mingw32-winssl-zlib-ipv6 LDFLAGS=-static
strip -s lib\libcurl.dll

mkdir %INSTALL_DIR%\bin64 %INSTALL_DIR%\lib64
copy lib\libcurl.dll %INSTALL_DIR%\bin64
pexports lib\libcurl.dll > curl.def
lib /MACHINE:X64 /DEF:curl.def /OUT:%INSTALL_DIR%\lib64\curl.lib
cd ..

: Build sample
SET PATH=%INSTALL_DIR%\bin64;%INSTALL_DIR%\bin
dmd -m64 app.d -ofapp64.exe
app64

Downloading Files with cURL

As we saw, cURL directly downloads the URL content and prints it to the terminal. However, if you want to save the output as a file, you can specify a filename with the  option, like so:

curl -o vlc.dmg http://ftp.belnet.be/mirror/videolan/vlc/3.0.4/macosx/vlc-3.0.4.dmg

In addition to saving the contents, cURL switches to displaying a nice progress bar with download statistics, such as the speed and the time taken:

Instead of providing a file name manually, you can let cURL figure out the filename with the  option. So, if you want to save the above URL to the file , you can simply use:

curl -O http://ftp.belnet.be/mirror/videolan/vlc/3.0.4/macosx/vlc-3.0.4.dmg

Bear in mind that when you use the  or the  options and a file of the same name exists, cURL will overwrite it.

If you have a partially downloaded file, you can resume the file download with the  option, as shown below:

curl -O -C - http://ftp.belnet.be/mirror/videolan/vlc/3.0.4/macosx/vlc-3.0.4.dmg

Like most other command line tools, you can combine different options together. For example, in the above command, you could combine  and write it as  .

Несколько примеров на Guzzle

GET запросы на Guzzle

// Создаем клиента с базовым URL
$client = new GuzzleHttp\Client(['base_uri' => 'https://foo.com/api/']);
// Посылаем запрос на https://foo.com/api/test
$response = $client->request('GET', 'test');
// Посылаем запрос на https://foo.com/root
$response = $client->request('GET', '/root');

Разные типы запросов на Guzzle

$response = $client->get('http://httpbin.org/get');
$response = $client->delete('http://httpbin.org/delete');
$response = $client->head('http://httpbin.org/get');
$response = $client->options('http://httpbin.org/get');
$response = $client->patch('http://httpbin.org/patch');
$response = $client->post('http://httpbin.org/post');
$response = $client->put('http://httpbin.org/put');

Асинхронные запросы на Guzzle

$promise = $client->getAsync('http://httpbin.org/get');
$promise = $client->deleteAsync('http://httpbin.org/delete');
$promise = $client->headAsync('http://httpbin.org/get');
$promise = $client->optionsAsync('http://httpbin.org/get');
$promise = $client->patchAsync('http://httpbin.org/patch');
$promise = $client->postAsync('http://httpbin.org/post');
$promise = $client->putAsync('http://httpbin.org/put');

Если интересно, то читайте: Guzzle Quick Start

Пишите комментарии, если что-то осталось непонятно.

Помогла ли Вам эта статья?

Да
Нет

HTTP OPTIONS

CURLOPT_AUTOREFERER

Automatically set Referer: header. See CURLOPT_AUTOREFERER

CURLOPT_ACCEPT_ENCODING

Accept-Encoding and automatic decompressing data. See CURLOPT_ACCEPT_ENCODING

CURLOPT_TRANSFER_ENCODING

Request Transfer-Encoding. See CURLOPT_TRANSFER_ENCODING

CURLOPT_FOLLOWLOCATION

Follow HTTP redirects. See CURLOPT_FOLLOWLOCATION

CURLOPT_UNRESTRICTED_AUTH

Do not restrict authentication to original host. CURLOPT_UNRESTRICTED_AUTH

CURLOPT_MAXREDIRS

Maximum number of redirects to follow. See CURLOPT_MAXREDIRS

CURLOPT_POSTREDIR

How to act on redirects after POST. See CURLOPT_POSTREDIR

CURLOPT_PUT

Issue an HTTP PUT request. See CURLOPT_PUT

CURLOPT_POST

Issue an HTTP POST request. See CURLOPT_POST

CURLOPT_POSTFIELDS

Send a POST with this data. See CURLOPT_POSTFIELDS

CURLOPT_POSTFIELDSIZE

The POST data is this big. See CURLOPT_POSTFIELDSIZE

CURLOPT_POSTFIELDSIZE_LARGE

The POST data is this big. See CURLOPT_POSTFIELDSIZE_LARGE

CURLOPT_COPYPOSTFIELDS

Send a POST with this data — and copy it. See CURLOPT_COPYPOSTFIELDS

CURLOPT_HTTPPOST

Multipart formpost HTTP POST. See CURLOPT_HTTPPOST

CURLOPT_REFERER

Referer: header. See CURLOPT_REFERER

CURLOPT_USERAGENT

User-Agent: header. See CURLOPT_USERAGENT

CURLOPT_HTTPHEADER

Custom HTTP headers. See CURLOPT_HTTPHEADER

CURLOPT_HEADEROPT

Control custom headers. See CURLOPT_HEADEROPT

CURLOPT_PROXYHEADER

Custom HTTP headers sent to proxy. See CURLOPT_PROXYHEADER

CURLOPT_HTTP200ALIASES

Alternative versions of 200 OK. See CURLOPT_HTTP200ALIASES

CURLOPT_COOKIE

Cookie(s) to send. See CURLOPT_COOKIE

CURLOPT_COOKIEFILE

File to read cookies from. See CURLOPT_COOKIEFILE

CURLOPT_COOKIEJAR

File to write cookies to. See CURLOPT_COOKIEJAR

CURLOPT_COOKIESESSION

Start a new cookie session. See CURLOPT_COOKIESESSION

CURLOPT_COOKIELIST

Add or control cookies. See CURLOPT_COOKIELIST

CURLOPT_ALTSVC

Specify the Alt-Svc: cache file name. See CURLOPT_ALTSVC

CURLOPT_ALTSVC_CTRL

Enable and configure Alt-Svc: treatment. See CURLOPT_ALTSVC_CTRL

CURLOPT_HSTS

Set HSTS cache file. See CURLOPT_HSTS

CURLOPT_HSTS_CTRL

Enable HSTS. See CURLOPT_HSTS_CTRL

CURLOPT_HSTSREADFUNCTION

Set HSTS read callback. See CURLOPT_HSTSREADFUNCTION

CURLOPT_HSTSREADDATA

Pass pointer to the HSTS read callback. See CURLOPT_HSTSREADDATA

CURLOPT_HSTSWRITEFUNCTION

Set HSTS write callback. See CURLOPT_HSTSWRITEFUNCTION

CURLOPT_HSTSWRITEDATA

Pass pointer to the HSTS write callback. See CURLOPT_HSTSWRITEDATA

CURLOPT_HTTPGET

Do an HTTP GET request. See CURLOPT_HTTPGET

CURLOPT_REQUEST_TARGET

Set the request target. CURLOPT_REQUEST_TARGET

CURLOPT_HTTP_VERSION

HTTP version to use. CURLOPT_HTTP_VERSION

CURLOPT_HTTP09_ALLOWED

Allow HTTP/0.9 responses. CURLOPT_HTTP09_ALLOWED

CURLOPT_IGNORE_CONTENT_LENGTH

Ignore Content-Length. See CURLOPT_IGNORE_CONTENT_LENGTH

CURLOPT_HTTP_CONTENT_DECODING

Disable Content decoding. See CURLOPT_HTTP_CONTENT_DECODING

CURLOPT_HTTP_TRANSFER_DECODING

Disable Transfer decoding. See CURLOPT_HTTP_TRANSFER_DECODING

CURLOPT_EXPECT_100_TIMEOUT_MS

100-continue timeout. See CURLOPT_EXPECT_100_TIMEOUT_MS

CURLOPT_TRAILERFUNCTION

Set callback for sending trailing headers. See CURLOPT_TRAILERFUNCTION

CURLOPT_TRAILERDATA

Custom pointer passed to the trailing headers callback. See CURLOPT_TRAILERDATA

CURLOPT_PIPEWAIT

Wait on connection to pipeline on it. See CURLOPT_PIPEWAIT

CURLOPT_STREAM_DEPENDS

This HTTP/2 stream depends on another. See CURLOPT_STREAM_DEPENDS

CURLOPT_STREAM_DEPENDS_E

This HTTP/2 stream depends on another exclusively. See CURLOPT_STREAM_DEPENDS_E

CURLOPT_STREAM_WEIGHT

Set this HTTP/2 stream’s weight. See CURLOPT_STREAM_WEIGHT

Использования cURL онлайн

Да, это возможно. Вы можете выполнить cURL удаленно с помощью следующих инструментов.Online CURL — компактный инструмент для извлечения URL-адреса онлайн и добавления следующих параметров.

--connect-timeout
--cookie
--data
--header
--head
--location
--max-time
--proxy
--request
--user
--url
--user-agent

Пример вывода:

cURL command line builder–позволяет создать команду cURL, с помощью которой можно ввести информацию в пользовательский интерфейс.

cURL — полезная утилита для устранения проблем с подключением в режиме реального времени.

Пожалуйста, оставьте свои отзывы по текущей теме статьи. За комментарии, отклики, подписки, лайки, дизлайки низкий вам поклон!

Базовая структура

Прежде чем мы двинемся дальше к более сложным примерам, давайте рассмотрим базовую структуру cURL запроса в PHP. Существует четыре основных шага, которые нужно учитывать в каждом скрипте:

  • Инициализация
  • Назначение параметров
  • Выполнение и выборка результата
  • Освобождение памяти
// 1. инициализация
$ch = curl_init();

// 2. указываем параметры, включая url
curl_setopt($ch, CURLOPT_URL, "http://www.nettuts.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);

// 3. получаем HTML в качестве результата
$output = curl_exec($ch);

// 4. закрываем соединение
curl_close($ch);

Шаг #2 (то есть, вызов curl_setopt()) будем обсуждать в этой статье намного больше, чем все другие этапы, т.к. на этой стадии происходит всё самое интересное и полезное, что вам необходимо знать. В cURL существует огромное количество различных опций, которые должны быть указаны, для того чтобы иметь возможность сконфигурировать URL-запрос самым тщательным образом. Мы не будем рассматривать весь список целиком, а остановимся только на том, что я посчитаю нужным и полезным для этого урока. Всё остальное вы сможете изучить сами, если эта тема вас заинтересует.

FTP OPTIONS

CURLOPT_FTPPORT

Use active FTP. See CURLOPT_FTPPORT

CURLOPT_QUOTE

Commands to run before transfer. See CURLOPT_QUOTE

CURLOPT_POSTQUOTE

Commands to run after transfer. See CURLOPT_POSTQUOTE

CURLOPT_PREQUOTE

Commands to run just before transfer. See CURLOPT_PREQUOTE

CURLOPT_APPEND

Append to remote file. See CURLOPT_APPEND

CURLOPT_FTP_USE_EPRT

Use EPTR. See CURLOPT_FTP_USE_EPRT

CURLOPT_FTP_USE_EPSV

Use EPSV. See CURLOPT_FTP_USE_EPSV

CURLOPT_FTP_USE_PRET

Use PRET. See CURLOPT_FTP_USE_PRET

CURLOPT_FTP_CREATE_MISSING_DIRS

Create missing directories on the remote server. See CURLOPT_FTP_CREATE_MISSING_DIRS

CURLOPT_FTP_RESPONSE_TIMEOUT

Timeout for FTP responses. See CURLOPT_FTP_RESPONSE_TIMEOUT

CURLOPT_FTP_ALTERNATIVE_TO_USER

Alternative to USER. See CURLOPT_FTP_ALTERNATIVE_TO_USER

CURLOPT_FTP_SKIP_PASV_IP

Ignore the IP address in the PASV response. See CURLOPT_FTP_SKIP_PASV_IP

CURLOPT_FTPSSLAUTH

Control how to do TLS. See CURLOPT_FTPSSLAUTH

CURLOPT_FTP_SSL_CCC

Back to non-TLS again after authentication. See CURLOPT_FTP_SSL_CCC

CURLOPT_FTP_ACCOUNT

Send ACCT command. See CURLOPT_FTP_ACCOUNT

CURLOPT_FTP_FILEMETHOD

Specify how to reach files. See CURLOPT_FTP_FILEMETHOD

Building from git

If you get your code off a git repository instead of a release tarball, see the file in the root directory for specific instructions on how to proceed.

Unix

A normal Unix installation is made in three or four steps (after you’ve unpacked the source archive):

(Adjust the configure line accordingly to use the TLS library you want.)

You probably need to be root when doing the last command.

Get a full listing of all available configure options by invoking it like:

If you want to install curl in a different file hierarchy than , specify that when running configure:

If you have write permission in that directory, you can do ‘make install’ without being root. An example of this would be to make a local install in your own home directory:

The configure script always tries to find a working SSL library unless explicitly told not to. If you have OpenSSL installed in the default search path for your compiler/linker, you don’t need to do anything special. If you have OpenSSL installed in , you can run configure like:

If you have OpenSSL installed somewhere else (for example, ) and you have pkg-config installed, set the pkg-config path first, like this:

Without pkg-config installed, use this:

If you insist on forcing a build without SSL support, even though you may have OpenSSL installed in your system, you can run configure like this:

If you have OpenSSL installed, but with the libraries in one place and the header files somewhere else, you have to set the and environment variables prior to running configure. Something like this should work:

If you have shared SSL libs installed in a directory where your run-time linker doesn’t find them (which usually causes configure failures), you can provide this option to gcc to set a hard-coded path to the run-time linker:

CONNECTION OPTIONS

CURLOPT_TIMEOUT

Timeout for the entire request. See CURLOPT_TIMEOUT

CURLOPT_TIMEOUT_MS

Millisecond timeout for the entire request. See CURLOPT_TIMEOUT_MS

CURLOPT_LOW_SPEED_LIMIT

Low speed limit to abort transfer. See CURLOPT_LOW_SPEED_LIMIT

CURLOPT_LOW_SPEED_TIME

Time to be below the speed to trigger low speed abort. See CURLOPT_LOW_SPEED_TIME

CURLOPT_MAX_SEND_SPEED_LARGE

Cap the upload speed to this. See CURLOPT_MAX_SEND_SPEED_LARGE

CURLOPT_MAX_RECV_SPEED_LARGE

Cap the download speed to this. See CURLOPT_MAX_RECV_SPEED_LARGE

CURLOPT_MAXCONNECTS

Maximum number of connections in the connection pool. See CURLOPT_MAXCONNECTS

CURLOPT_FRESH_CONNECT

Use a new connection. CURLOPT_FRESH_CONNECT

CURLOPT_FORBID_REUSE

Prevent subsequent connections from re-using this. See CURLOPT_FORBID_REUSE

CURLOPT_MAXAGE_CONN

Limit the age of connections for reuse. See CURLOPT_MAXAGE_CONN

CURLOPT_CONNECTTIMEOUT

Timeout for the connection phase. See CURLOPT_CONNECTTIMEOUT

CURLOPT_CONNECTTIMEOUT_MS

Millisecond timeout for the connection phase. See CURLOPT_CONNECTTIMEOUT_MS

CURLOPT_IPRESOLVE

IP version to use. See CURLOPT_IPRESOLVE

CURLOPT_CONNECT_ONLY

Only connect, nothing else. See CURLOPT_CONNECT_ONLY

CURLOPT_USE_SSL

Use TLS/SSL. See CURLOPT_USE_SSL

CURLOPT_RESOLVE

Provide fixed/fake name resolves. See CURLOPT_RESOLVE

CURLOPT_DNS_INTERFACE

Bind name resolves to this interface. See CURLOPT_DNS_INTERFACE

CURLOPT_DNS_LOCAL_IP4

Bind name resolves to this IP4 address. See CURLOPT_DNS_LOCAL_IP4

CURLOPT_DNS_LOCAL_IP6

Bind name resolves to this IP6 address. See CURLOPT_DNS_LOCAL_IP6

CURLOPT_DNS_SERVERS

Preferred DNS servers. See CURLOPT_DNS_SERVERS

CURLOPT_DNS_SHUFFLE_ADDRESSES

Shuffle addresses before use. See CURLOPT_DNS_SHUFFLE_ADDRESSES

CURLOPT_ACCEPTTIMEOUT_MS

Timeout for waiting for the server’s connect back to be accepted. See CURLOPT_ACCEPTTIMEOUT_MS

CURLOPT_HAPPY_EYEBALLS_TIMEOUT_MS

Timeout for happy eyeballs. See CURLOPT_HAPPY_EYEBALLS_TIMEOUT_MS

CURLOPT_UPKEEP_INTERVAL_MS

Sets the interval at which connection upkeep are performed. See CURLOPT_UPKEEP_INTERVAL_MS

FTP and Firewalls

The FTP protocol requires one of the involved parties to open a second connection as soon as data is about to get transferred. There are two ways to do this.

The default way for curl is to issue the PASV command which causes the server to open another port and await another connection performed by the client. This is good if the client is behind a firewall that doesn’t allow incoming connections.

If the server, for example, is behind a firewall that doesn’t allow connections on ports other than 21 (or if it just doesn’t support the command), the other way to do it is to use the command and instruct the server to connect to the client on the given IP number and port (as parameters to the PORT command).

The flag to curl supports a few different options. Your machine may have several IP-addresses and/or network interfaces and curl allows you to select which of them to use. Default address can also be used:

Download with but use the IP address of our interface (this does not work on windows):

Download with but use 192.168.0.10 as our IP address to use:

POST

The GET method makes all input field names get displayed in the URL field of your browser. That’s generally a good thing when you want to be able to bookmark that page with your given data, but it is an obvious disadvantage if you entered secret information in one of the fields or if there are a large amount of fields creating a very long and unreadable URL.

The HTTP protocol then offers the POST method. This way the client sends the data separated from the URL and thus you won’t see any of it in the URL address field.

The form would look very similar to the previous one:

And to use curl to post this form with the same data filled in as before, we could do it like:

This kind of POST will use the Content-Type and is the most widely used POST kind.

The data you send to the server MUST already be properly encoded, curl will not do that for you. For example, if you want the data to contain a space, you need to replace that space with , etc. Failing to comply with this will most likely cause your data to be received wrongly and messed up.

Recent curl versions can in fact url-encode POST data for you, like this:

If you repeat several times on the command line, curl will concatenate all the given data pieces — and put a symbol between each data segment.

cURL configuration files

Sometimes, you may want to make all cURL requests use the same options. Passing these options by hand isn’t a feasible solution, so cURL allows you to specify options in a configuration file.

The default configuration file is located in  in Linux/MacOS and  in Windows. Inside this file, you can specify any options that you need, such as:

# Always use IPv4
-4
# Always show verbose output
-v
# When following a redirect, automatically set the previous URL as referer.
referer = ";auto"
# Wait 60 seconds before timing out.
connect-timeout = 60

After creating the above file, try making a request with . You will find that these options have taken effect.

If you want to use a custom configuration file instead of the default one, then you can use  option to point curl to your configuration file. As an example, if you have a configuration file called , then you can use it with:

curl -K config.txt example.com
Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *

Adblock
detector