From e84320ad75efaaadd67a03fb816eeafa7fea2d98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20=C3=87al=C4=B1=C5=9Fkan?= <13554944+thecaliskan@users.noreply.github.com> Date: Wed, 16 Jul 2025 12:08:19 +0300 Subject: [PATCH] Add support for CURLINFO_CONN_ID in curl_getinfo() (#18984) This patch adds support for the CURLINFO_CONN_ID constant in the curl_getinfo() function when compiled with libcurl >= 8.2.0. CURLINFO_CONN_ID allows retrieving the unique identifier of the underlying connection used in the most recent transfer. This is useful for advanced features like connection reuse tracking, diagnostics, or connection pooling implementations at the PHP level. --- UPGRADING | 6 + ext/curl/curl.stub.php | 7 + ext/curl/curl_arginfo.h | 5 +- ext/curl/interface.c | 5 + .../tests/curl_getinfo_CURLINFO_CONN_ID.phpt | 146 ++++++++++++++++++ 5 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 ext/curl/tests/curl_getinfo_CURLINFO_CONN_ID.phpt diff --git a/UPGRADING b/UPGRADING index 2a829755171..1d878a44e2a 100644 --- a/UPGRADING +++ b/UPGRADING @@ -188,6 +188,11 @@ PHP 8.5 UPGRADE NOTES first redirect thus if there is any follow up redirect, it won't go any further. CURLFOLLOW_ALL is equivalent to setting CURLOPT_FOLLOWLOCATION to true. + . Added support for CURLINFO_CONN_ID (libcurl >= 8.2.0) to the curl_getinfo() + function. This constant allows retrieving the unique ID of the connection + used by a cURL transfer. It is primarily useful when connection reuse or + connection pooling logic is needed in PHP-level applications. When + curl_getinfo() returns an array, this value is available as the "conn_id" key. - DOM: . Added Dom\Element::$outerHTML. @@ -517,6 +522,7 @@ PHP 8.5 UPGRADE NOTES . CURLINFO_USED_PROXY. . CURLINFO_HTTPAUTH_USED. . CURLINFO_PROXYAUTH_USED. + . CURLINFO_CONN_ID. . CURLOPT_INFILESIZE_LARGE. . CURLFOLLOW_ALL. . CURLFOLLOW_OBEYCODE. diff --git a/ext/curl/curl.stub.php b/ext/curl/curl.stub.php index 00532f45793..5dac22ae795 100644 --- a/ext/curl/curl.stub.php +++ b/ext/curl/curl.stub.php @@ -3103,6 +3103,13 @@ const CURLINFO_USED_PROXY = UNKNOWN; */ const CURLINFO_POSTTRANSFER_TIME_T = UNKNOWN; #endif +#if LIBCURL_VERSION_NUM >= 0x080200 /* Available since 8.2.0 */ +/** + * @var int + * @cvalue CURLINFO_CONN_ID + */ +const CURLINFO_CONN_ID = UNKNOWN; +#endif /** * @var int * @cvalue CURLOPT_DISALLOW_USERNAME_IN_URL diff --git a/ext/curl/curl_arginfo.h b/ext/curl/curl_arginfo.h index 1defefdab2d..75be3b75c49 100644 --- a/ext/curl/curl_arginfo.h +++ b/ext/curl/curl_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: c087ac501d0abe14ed87968023d837f358e6fee8 */ + * Stub hash: 533884c442ca1146a61c0824a4f8c9628c31aae3 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_curl_close, 0, 1, IS_VOID, 0) ZEND_ARG_OBJ_INFO(0, handle, CurlHandle, 0) @@ -824,6 +824,9 @@ static void register_curl_symbols(int module_number) #endif #if LIBCURL_VERSION_NUM >= 0x080a00 /* Available since 8.10.0 */ REGISTER_LONG_CONSTANT("CURLINFO_POSTTRANSFER_TIME_T", CURLINFO_POSTTRANSFER_TIME_T, CONST_PERSISTENT); +#endif +#if LIBCURL_VERSION_NUM >= 0x080200 /* Available since 8.2.0 */ + REGISTER_LONG_CONSTANT("CURLINFO_CONN_ID", CURLINFO_CONN_ID, CONST_PERSISTENT); #endif REGISTER_LONG_CONSTANT("CURLOPT_DISALLOW_USERNAME_IN_URL", CURLOPT_DISALLOW_USERNAME_IN_URL, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("CURLOPT_PROXY_TLS13_CIPHERS", CURLOPT_PROXY_TLS13_CIPHERS, CONST_PERSISTENT); diff --git a/ext/curl/interface.c b/ext/curl/interface.c index 99bdf373318..ffb7889c748 100644 --- a/ext/curl/interface.c +++ b/ext/curl/interface.c @@ -2622,6 +2622,11 @@ PHP_FUNCTION(curl_getinfo) if (curl_easy_getinfo(ch->cp, CURLINFO_PROXYAUTH_USED, &l_code) == CURLE_OK) { CAAL("proxyauth_used", l_code); } +#endif +#if LIBCURL_VERSION_NUM >= 0x080200 /* Available since 8.2.0 */ + if (curl_easy_getinfo(ch->cp, CURLINFO_CONN_ID , &co) == CURLE_OK) { + CAAL("conn_id", co); + } #endif } else { switch (option) { diff --git a/ext/curl/tests/curl_getinfo_CURLINFO_CONN_ID.phpt b/ext/curl/tests/curl_getinfo_CURLINFO_CONN_ID.phpt new file mode 100644 index 00000000000..4a90b0a3c52 --- /dev/null +++ b/ext/curl/tests/curl_getinfo_CURLINFO_CONN_ID.phpt @@ -0,0 +1,146 @@ +--TEST-- +Curlinfo CURLINFO_CONN_ID +--EXTENSIONS-- +curl +--SKIPIF-- += 8.2.0"); +?> +--FILE-- +0); + +foreach([$ch1, $ch2] as $key => $ch) { + $result = curl_multi_getcontent($ch); + $info = curl_getinfo($ch); + var_dump(isset($info['conn_id'])); + var_dump(is_int($info['conn_id'])); + var_dump(curl_getinfo($ch, CURLINFO_CONN_ID) === $info['conn_id']); + var_dump(curl_getinfo($ch, CURLINFO_CONN_ID) === $key); +} + +$csh = curl_share_init(); + +curl_share_setopt($csh, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE); +curl_share_setopt($csh, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT); +curl_share_setopt($csh, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS); +curl_share_setopt($csh, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION); + + +$ch1=curl_init(); +$ch2=curl_init(); + +foreach([$ch1, $ch2] as $ch) { + curl_setopt($ch, CURLOPT_URL, "{$host}/get.inc?test=getpost&get_param=Curl%20Handle"); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $info = curl_getinfo($ch); + var_dump(isset($info['conn_id'])); + var_dump($info['conn_id'] === -1); +} + + +curl_setopt($ch1, CURLOPT_SHARE, $csh); + +$result = curl_exec($ch1); + +$info = curl_getinfo($ch1); +var_dump(isset($info['conn_id'])); +var_dump(is_int($info['conn_id'])); +var_dump(curl_getinfo($ch1, CURLINFO_CONN_ID) === $info['conn_id']); +var_dump(curl_getinfo($ch1, CURLINFO_CONN_ID) === 0); + +curl_setopt($ch2, CURLOPT_SHARE, $csh); + +$result = curl_exec($ch2); + +$info = curl_getinfo($ch2); +var_dump(isset($info['conn_id'])); +var_dump(is_int($info['conn_id'])); +var_dump(curl_getinfo($ch2, CURLINFO_CONN_ID) === $info['conn_id']); +var_dump(curl_getinfo($ch2, CURLINFO_CONN_ID) === 1); + +?> +--EXPECT-- +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +