Skip to content

Commit e84320a

Browse files
authored
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.
1 parent 2ecafd4 commit e84320a

File tree

5 files changed

+168
-1
lines changed

5 files changed

+168
-1
lines changed

UPGRADING

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@ PHP 8.5 UPGRADE NOTES
188188
first redirect thus if there is any follow up redirect, it won't go
189189
any further. CURLFOLLOW_ALL is equivalent to setting CURLOPT_FOLLOWLOCATION
190190
to true.
191+
. Added support for CURLINFO_CONN_ID (libcurl >= 8.2.0) to the curl_getinfo()
192+
function. This constant allows retrieving the unique ID of the connection
193+
used by a cURL transfer. It is primarily useful when connection reuse or
194+
connection pooling logic is needed in PHP-level applications. When
195+
curl_getinfo() returns an array, this value is available as the "conn_id" key.
191196

192197
- DOM:
193198
. Added Dom\Element::$outerHTML.
@@ -517,6 +522,7 @@ PHP 8.5 UPGRADE NOTES
517522
. CURLINFO_USED_PROXY.
518523
. CURLINFO_HTTPAUTH_USED.
519524
. CURLINFO_PROXYAUTH_USED.
525+
. CURLINFO_CONN_ID.
520526
. CURLOPT_INFILESIZE_LARGE.
521527
. CURLFOLLOW_ALL.
522528
. CURLFOLLOW_OBEYCODE.

ext/curl/curl.stub.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3103,6 +3103,13 @@
31033103
*/
31043104
const CURLINFO_POSTTRANSFER_TIME_T = UNKNOWN;
31053105
#endif
3106+
#if LIBCURL_VERSION_NUM >= 0x080200 /* Available since 8.2.0 */
3107+
/**
3108+
* @var int
3109+
* @cvalue CURLINFO_CONN_ID
3110+
*/
3111+
const CURLINFO_CONN_ID = UNKNOWN;
3112+
#endif
31063113
/**
31073114
* @var int
31083115
* @cvalue CURLOPT_DISALLOW_USERNAME_IN_URL

ext/curl/curl_arginfo.h

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/curl/interface.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,6 +2622,11 @@ PHP_FUNCTION(curl_getinfo)
26222622
if (curl_easy_getinfo(ch->cp, CURLINFO_PROXYAUTH_USED, &l_code) == CURLE_OK) {
26232623
CAAL("proxyauth_used", l_code);
26242624
}
2625+
#endif
2626+
#if LIBCURL_VERSION_NUM >= 0x080200 /* Available since 8.2.0 */
2627+
if (curl_easy_getinfo(ch->cp, CURLINFO_CONN_ID , &co) == CURLE_OK) {
2628+
CAAL("conn_id", co);
2629+
}
26252630
#endif
26262631
} else {
26272632
switch (option) {
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
--TEST--
2+
Curlinfo CURLINFO_CONN_ID
3+
--EXTENSIONS--
4+
curl
5+
--SKIPIF--
6+
<?php
7+
$curl_version = curl_version();
8+
if ($curl_version['version_number'] < 0x080200) die("skip: test works only with curl >= 8.2.0");
9+
?>
10+
--FILE--
11+
<?php
12+
include 'server.inc';
13+
14+
$host = curl_cli_server_start();
15+
$port = (int) (explode(':', $host))[1];
16+
17+
$ch = curl_init();
18+
curl_setopt($ch, CURLOPT_URL, "{$host}/get.inc?test=get");
19+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
20+
21+
$info = curl_getinfo($ch);
22+
var_dump(isset($info['conn_id']));
23+
var_dump($info['conn_id'] === -1);
24+
25+
$result = curl_exec($ch);
26+
27+
$info = curl_getinfo($ch);
28+
var_dump(isset($info['conn_id']));
29+
var_dump(is_int($info['conn_id']));
30+
var_dump(curl_getinfo($ch, CURLINFO_CONN_ID) === $info['conn_id']);
31+
var_dump(curl_getinfo($ch, CURLINFO_CONN_ID) === 0);
32+
33+
curl_setopt($ch, CURLOPT_URL, "{$host}/get.inc?test=get");
34+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
35+
$result = curl_exec($ch);
36+
37+
$info = curl_getinfo($ch);
38+
var_dump(isset($info['conn_id']));
39+
var_dump(is_int($info['conn_id']));
40+
var_dump(curl_getinfo($ch, CURLINFO_CONN_ID) === $info['conn_id']);
41+
var_dump(curl_getinfo($ch, CURLINFO_CONN_ID) === 1);
42+
43+
$ch1=curl_init();
44+
$ch2=curl_init();
45+
$cmh=curl_multi_init();
46+
47+
foreach([$ch1, $ch2] as $ch) {
48+
curl_setopt($ch, CURLOPT_URL, "{$host}/get.inc?test=getpost&get_param=Curl%20Handle");
49+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
50+
$info = curl_getinfo($ch);
51+
var_dump(isset($info['conn_id']));
52+
var_dump($info['conn_id'] === -1);
53+
curl_multi_add_handle($cmh,$ch);
54+
}
55+
56+
$running=0;
57+
do {
58+
curl_multi_exec($cmh,$running);
59+
} while ($running>0);
60+
61+
foreach([$ch1, $ch2] as $key => $ch) {
62+
$result = curl_multi_getcontent($ch);
63+
$info = curl_getinfo($ch);
64+
var_dump(isset($info['conn_id']));
65+
var_dump(is_int($info['conn_id']));
66+
var_dump(curl_getinfo($ch, CURLINFO_CONN_ID) === $info['conn_id']);
67+
var_dump(curl_getinfo($ch, CURLINFO_CONN_ID) === $key);
68+
}
69+
70+
$csh = curl_share_init();
71+
72+
curl_share_setopt($csh, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
73+
curl_share_setopt($csh, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
74+
curl_share_setopt($csh, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
75+
curl_share_setopt($csh, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
76+
77+
78+
$ch1=curl_init();
79+
$ch2=curl_init();
80+
81+
foreach([$ch1, $ch2] as $ch) {
82+
curl_setopt($ch, CURLOPT_URL, "{$host}/get.inc?test=getpost&get_param=Curl%20Handle");
83+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
84+
$info = curl_getinfo($ch);
85+
var_dump(isset($info['conn_id']));
86+
var_dump($info['conn_id'] === -1);
87+
}
88+
89+
90+
curl_setopt($ch1, CURLOPT_SHARE, $csh);
91+
92+
$result = curl_exec($ch1);
93+
94+
$info = curl_getinfo($ch1);
95+
var_dump(isset($info['conn_id']));
96+
var_dump(is_int($info['conn_id']));
97+
var_dump(curl_getinfo($ch1, CURLINFO_CONN_ID) === $info['conn_id']);
98+
var_dump(curl_getinfo($ch1, CURLINFO_CONN_ID) === 0);
99+
100+
curl_setopt($ch2, CURLOPT_SHARE, $csh);
101+
102+
$result = curl_exec($ch2);
103+
104+
$info = curl_getinfo($ch2);
105+
var_dump(isset($info['conn_id']));
106+
var_dump(is_int($info['conn_id']));
107+
var_dump(curl_getinfo($ch2, CURLINFO_CONN_ID) === $info['conn_id']);
108+
var_dump(curl_getinfo($ch2, CURLINFO_CONN_ID) === 1);
109+
110+
?>
111+
--EXPECT--
112+
bool(true)
113+
bool(true)
114+
bool(true)
115+
bool(true)
116+
bool(true)
117+
bool(true)
118+
bool(true)
119+
bool(true)
120+
bool(true)
121+
bool(true)
122+
bool(true)
123+
bool(true)
124+
bool(true)
125+
bool(true)
126+
bool(true)
127+
bool(true)
128+
bool(true)
129+
bool(true)
130+
bool(true)
131+
bool(true)
132+
bool(true)
133+
bool(true)
134+
bool(true)
135+
bool(true)
136+
bool(true)
137+
bool(true)
138+
bool(true)
139+
bool(true)
140+
bool(true)
141+
bool(true)
142+
bool(true)
143+
bool(true)
144+
bool(true)
145+
bool(true)
146+

0 commit comments

Comments
 (0)