Sending Curl Keep-Alive Request [PHP Code]

Curl keeps connections open and reuses existing ones when possible. If you need to pass the Connection: keep-alive header to Curl, you can use the -H command line option. The Keep-Alive Connection means the server will not close the Connection after making the request. HTTP clients may specify the additional persistent connection options in a separate - H "Keep-Alive: [options]" HTTP header. In this Curl Keep-AliveConnection Example, we send a keep-alive request to the ReqBin echo URL. Click Run to execute the Curl Command with Keep-Alive Connection Header online and see the results. The PHP code was automatically generated for the Curl Keep Alive Connection Header example.
Sending Curl Keep-Alive Request [PHP Code] Run
curl https://reqbin.com/echo
     -H "Connection: keep-alive"
     -H "Keep-Alive: timeout=5, max=100"
Updated: Viewed: 34092 times
PHP code for Curl Keep Alive Connection Header example

PHP code for Curl Keep Alive Connection Header Example

This PHP code snippet was generated automatically for the Curl Keep Alive Connection Header example.
<< Back to the Curl Keep Alive Connection Header example

What is Curl?

Curl is a command-line utility available for Linux, Windows, and macOS, and that allows sending HTTP requests to the server with different parameters and methods. Curl supports over 25+ protocols, including HTTP, HTTPS, FTP, and has built-in support for HTTP Cookies, web forms, SSL, user authentication, and proxies.

What is Connection Header?

The Connection header determines whether the current TCP connection remains open after a transaction. Persistent connections allow you to send multiple requests without opening a new connection for each transaction. By default, connections are persistent for HTTP/1.1 clients. For HTTP/1.0 connections, you need to pass the "Connection: keep-alive" request header to indicate that you want a persistent connection.

Curl Connection Header Syntax

The following is the syntax of sending a Curl request with a Keep-Alive Connection Header:

Connection Header Syntax
curl -H "Connection: keep-alive" [URL]

What is Keep-Alive header?

The Keep-Alive header allows you to set the parameters of a persistent connection: connection timeout and the maximum number of requests in one connection.

Keep-Alive header Example
-H "Keep-Alive: timeout=5, max=100"

Where:
  • timeout: sets the maximum timeout to 5 seconds
  • max: sets the maximum number of requests per connection to 100

How to make a Keep-Alive request with Curl?

An example of sending a Curl request with directives for establishing a persistent connection and with connection parameters:

Keep-Alive Connection Example
curl https://reqbin.com/echo
   -H "Connection: keep-alive"
   -H "Keep-Alive: timeout=5, max=100"

HTTP versions support persistent connections

The table shows the difference between persistent connections in HTTP versions.

HTTP version Description
HTTP/1.0 In HTTP/1.0, connections are not persistent unless the client sends the -H "Connection: keep-alive" header. The server responds to persistent connections by sending the client the "Connection: keep-alive" header. The connection will then be open, and the client can send other requests through the same connection. Connections will remain open until they are dropped by either the client or server.
HTTP/1.1 HTTP/1.1 considers all connections persistent unless otherwise specified and does not require additional -H "Connection: keep-alive" headers.
HTTP/2 HTTP/2 does not allow connection and keep-alive headers. Chrome and Firefox ignore these headers in HTTP/2 responses, but Safari won't load a page with these headers.

See also

Generate code snippets for PHP and other programming languages

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