Skip to content

Commit c76a68f

Browse files
authored
feat: CURL option force_ip_resolve (#9194)
* feat: CURL option force_ip_resolve * test: CURL option force_ip_resolve * docs: CURL option force_ip_resolve * fix: PHPStan error * fix: prevent using empty() * fix: strict check * tests: unknown value in option force_ip_resolve * fix: using default match * fix: PHPUnit option resolved_force_ip
1 parent c46cea4 commit c76a68f

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

system/HTTP/CURLRequest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,15 @@ protected function setCURLOptions(array $curlOptions = [], array $config = [])
649649
$this->setHeader('Content-Length', (string) strlen($json));
650650
}
651651

652+
// Resolve IP
653+
if (array_key_exists('force_ip_resolve', $config)) {
654+
$curlOptions[CURLOPT_IPRESOLVE] = match ($config['force_ip_resolve']) {
655+
'v4' => CURL_IPRESOLVE_V4,
656+
'v6' => CURL_IPRESOLVE_V6,
657+
default => CURL_IPRESOLVE_WHATEVER
658+
};
659+
}
660+
652661
// version
653662
if (! empty($config['version'])) {
654663
$version = sprintf('%.1F', $config['version']);

tests/system/HTTP/CURLRequestTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,42 @@ public function testHTTPv3(): void
11761176
$this->assertSame(CURL_HTTP_VERSION_3, $options[CURLOPT_HTTP_VERSION]);
11771177
}
11781178

1179+
public function testForceResolveIPv4(): void
1180+
{
1181+
$this->request->request('POST', '/post', [
1182+
'force_ip_resolve' => 'v4',
1183+
]);
1184+
1185+
$options = $this->request->curl_options;
1186+
1187+
$this->assertArrayHasKey(CURLOPT_IPRESOLVE, $options);
1188+
$this->assertSame(CURL_IPRESOLVE_V4, $options[CURLOPT_IPRESOLVE]);
1189+
}
1190+
1191+
public function testForceResolveIPv6(): void
1192+
{
1193+
$this->request->request('POST', '/post', [
1194+
'force_ip_resolve' => 'v6',
1195+
]);
1196+
1197+
$options = $this->request->curl_options;
1198+
1199+
$this->assertArrayHasKey(CURLOPT_IPRESOLVE, $options);
1200+
$this->assertSame(CURL_IPRESOLVE_V6, $options[CURLOPT_IPRESOLVE]);
1201+
}
1202+
1203+
public function testForceResolveIPUnknown(): void
1204+
{
1205+
$this->request->request('POST', '/post', [
1206+
'force_ip_resolve' => 'v?',
1207+
]);
1208+
1209+
$options = $this->request->curl_options;
1210+
1211+
$this->assertArrayHasKey(CURLOPT_IPRESOLVE, $options);
1212+
$this->assertSame(\CURL_IPRESOLVE_WHATEVER, $options[CURLOPT_IPRESOLVE]);
1213+
}
1214+
11791215
public function testCookieOption(): void
11801216
{
11811217
$holder = SUPPORTPATH . 'HTTP/Files/CookiesHolder.txt';

user_guide_src/source/libraries/curlrequest.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,15 @@ is true:
364364

365365
.. _curlrequest-version:
366366

367+
force_ip_resolve
368+
================
369+
370+
.. versionadded:: 4.6.0
371+
372+
To set the HTTP handlers to use ``v4`` only ipv4 protocol or ``v6`` for ipv6 protocol:
373+
374+
.. literalinclude:: curlrequest/036.php
375+
367376
version
368377
=======
369378

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
// Force ipv4 resolve
4+
$client->request('GET', '/', ['force_ip_resolve' => 'v4']); // v4 or v6

0 commit comments

Comments
 (0)