PHP 8.4: Curl: curl_getinfo - CURLINFO_POSTTRANSFER_TIME_T support

Version8.4
TypeNew Feature

The curl_getinfo function in the Curl extension returns a list of information about a Curl request. It is also possible to pass a parameter to the function to retrieve a specific piece of information about the request.

As of Curl 8.10.0, Curl can return the time it took from the start until the last byte is sent (otherwise called "post time"). This can be a useful measurement in certain requests such as uploading a file to a remote HTTP form, uploading a file over FTP, etc.

PHP 8.4 exposes this feature if the system has libcurl 8.10.0 or later. It can be retrieved as the posttransfer_time_us key in curl_getinfo return value, or by passing the new CURLINFO_POSTTRANSFER_TIME_T constant to the curl_getinfo function.

posttransfer_time_us in curl_getinfo

When the curl_getinfo function is called without specifying the specific piece of information necessary, it returns an array of all available information about the request.

In PHP 8.4 and later, the return array contains a new posttransfer_time_us key that shows the time it took for the request to be sent, in microseconds.

$ch = curl_init('https://php.watch/versions/8.4/CURLINFO_POSTTRANSFER_TIME_T');
curl_exec($ch);

curl_getinfo($ch);
 [
     "url" => "https://php.watch/versions/8.4/CURLINFO_POSTTRANSFER_TIME_T",
     "content_type" => "text/html; charset=UTF-8",
     "http_code" => 200,
     // ...
    "total_time" => 1.669588,
    "namelookup_time" => 0.307668,
    "connect_time" => 0.615858,
     // ...
     "appconnect_time_us" => 966456,
     "connect_time_us" => 615858,
     "namelookup_time_us" => 307668,
     "pretransfer_time_us" => 966533,
     "redirect_time_us" => 0,
     "starttransfer_time_us" => 1271738,
+    "posttransfer_time_us" => 966578, // <== New!
     "total_time_us" => 1669588,
   ]

If the request was not sent, the posttransfer_time_us value remains 0. This follows the other ...time and ...time_us values remaining 0 if the request is not sent.

CURLINFO_POSTTRANSFER_TIME_T passed to curl_getinfo

PHP 8.4, compiled with libcurl 8.10.0 and later, provides a new constant named CURLINFO_POSTTRANSFER_TIME_T. It is one of the expected parameters for the curl_getinfo function that directly returns the same value as the posttransfer_time_us key.

When CURLINFO_POSTTRANSFER_TIME_T is passed to the second parameter of the curl_getinfo function, the return value will always be 0 or a positive integer.

$ch = curl_init('https://php.watch/versions/8.4/CURLINFO_POSTTRANSFER_TIME_T');
curl_exec($ch);

curl_getinfo($ch, CURLINFO_POSTTRANSFER_TIME_T);
// 966578

Backward Compatibility Impact

It is theoretically possible to retrieve this information on older PHP versions as well, as long as the Curl extension is compiled with libcurl 8.10.0 or later. The CURLINFO_POSTTRANSFER_TIME_T constant merely labels for the curl_getinfo function's second parameter.

The CURLINFO_POSTTRANSFER_TIME_T constant is assigned int value 6291523, and can be used directly with the curl_getinfo function:

curl_getinfo($ch, 6291523);

CURLINFO_POSTTRANSFER_TIME_T Implementation