PHP 8.4: Curl: New CURL_HTTP_VERSION_3 and CURL_HTTP_VERSION_3ONLY constants for HTTP/3 support

Version8.4
TypeNew Feature

PHP Curl extension is capable of making HTTP/3 (also known as QUIC) HTTP requests if the Curl extension is compiled with the necessary dependencies for HTTP/3.

The Curl extension in PHP 8.4 requires libcurl 7.61.0 or later. Curl HTTP/3 support requires libcurl 7.66.0 or later.

Since PHP 8.2 and later, it is possible to enable HTTP/3 support by passing the correct parameters to the CURLOPT_HTTP_VERSION Curl option.

In PHP 8.4, the Curl extension declares the two following new PHP constants, which are accepted parameters for the CURLOPT_HTTP_VERSION Curl option.

Note that it is possible to enable HTTP/3 on PHP 8.2 and later. The only difference in PHP 8.4 is that the above values are declared as PHP constants.

$ch = curl_init("https://php.watch/");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_3);
curl_exec($ch);

If the Curl extension is not built with HTTP/3 support, the curl_setopt call returns false and the CURLOPT_HTTP_VERSION option remains the default value or the existing value if previously set.

HTTP/3 Request with PHP Curl Extension How to make HTTP/3 HTTP requests using PHP Curl extension, along with details on how to compile Curl with HTTP/3 support for PHP.

CURL_HTTP_VERSION_3

When the CURLOPT_HTTP_VERSION Curl option is set to CURL_HTTP_VERSION_3, Curl will attempt to use HTTP/3 for the request, but will fallback to earlier HTTP versions if it fails. This is the typical and desirable way to enable HTTP/3 as it takes the opportunistic approach to enable HTTP/3.

The CURL_HTTP_VERSION_3 constant is assigned int value 30.

CURL_HTTP_VERSION_3ONLY

With CURLOPT_HTTP_VERSION Curl option set to CURL_HTTP_VERSION_3ONLY, Curl will attempt to use HTTP/3, but fail the request if the host does not support HTTP/3.

CURL_HTTP_VERSION_3ONLY constant is assigned int value 31.

Backward Compatibility Impact

CURL_HTTP_VERSION_3 and CURL_HTTP_VERSION_3ONLY are two new PHP constants declared in the global namespace. Unless a user-land PHP application declares identical constants, this change does not cause any backward compatibility issues.

On PHP 8.2 and later, it is possible to trivially polyfill the two new constants. Although not recommended, it is also possible to enable HTTP/3 support by directly passing the integer values of these constants.

if (curl_version()['version'] >= 0x075800) {
    define('CURL_HTTP_VERSION_3', 30);
    define('CURL_HTTP_VERSION_3ONLY', 31);
}

CURL_HTTP_VERSION_3 CURL_HTTP_VERSION_3ONLY Implementation