From ef62362cef932fedf42892299000cd3992ebb7e6 Mon Sep 17 00:00:00 2001 From: Steve Therrien Date: Fri, 7 Aug 2020 11:25:20 -0700 Subject: [PATCH] Added option to force IPv4 addresses when resolving hostnames (#45) --- README.md | 14 ++++++++++++++ src/Client.php | 15 +++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/README.md b/README.md index 9fd7745..b680c7c 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,20 @@ $client->setCAPath("/ca/path"); // if you need. *Note:* Certificate must match with host name in `Client` address (`localhost` in example above). +### DNS Resolution + +This error may indicate your system is having trouble resolving IPv6 addresses: + +``` +cURL error: Resolving timed out after [value] milliseconds +``` + +By default, both IPv4 and IPv6 addresses will attempt to be resolved. You can force it to only resolve IPv4 addresses with: + +```php +$client->forceIpResolveV4(); +``` + ### Testing Requirements: diff --git a/src/Client.php b/src/Client.php index b8e4310..e230b04 100644 --- a/src/Client.php +++ b/src/Client.php @@ -18,6 +18,7 @@ class Client private $connectTimeoutOption; private $timeoutOption; + private $forceIpResolveV4; private $safety = true; private $useAssoc = false; @@ -117,6 +118,17 @@ public function setTimeoutOption($timeoutOption) return $this; } + /** + * Forces DNS to only resolve IPv4 addresses. + * + * @return Client + */ + public function forceIpResolveV4() + { + $this->forceIpResolveV4 = true; + return $this; + } + /** * Publish data into channel. * @@ -381,6 +393,9 @@ private function request($method, $params) if ($this->timeoutOption) { curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeoutOption); } + if ($this->forceIpResolveV4) { + curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); + } if (!$this->safety) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);