diff --git a/.gitignore b/.gitignore index 3256fde..84ba485 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ vendor *.cache composer.lock -state.json \ No newline at end of file +state.json diff --git a/src/Client.php b/src/Client.php index 4714ce0..37659eb 100644 --- a/src/Client.php +++ b/src/Client.php @@ -25,8 +25,8 @@ class Client /** @var array headers */ private array $headers = []; - private int $timeout = 15; - private int $connectTimeout = 60; + private int $timeout = 15000; // milliseconds (15 seconds) + private int $connectTimeout = 60000; // milliseconds (60 seconds) private int $maxRedirects = 5; private bool $allowRedirects = true; private string $userAgent = ''; @@ -51,7 +51,7 @@ public function addHeader(string $key, string $value): self /** * Set the request timeout. * - * @param int $timeout + * @param int $timeout Timeout in milliseconds * @return self */ public function setTimeout(int $timeout): self @@ -87,7 +87,7 @@ public function setMaxRedirects(int $maxRedirects): self /** * Set the connection timeout. * - * @param int $connectTimeout + * @param int $connectTimeout Timeout in milliseconds * @return self */ public function setConnectTimeout(int $connectTimeout): self @@ -235,6 +235,8 @@ private function withRetries(callable $callback): mixed * @param array|array $body * @param array $query * @param ?callable $chunks Optional callback function that receives a Chunk object + * @param ?int $timeoutMs Optional request timeout in milliseconds + * @param ?int $connectTimeoutMs Optional connection timeout in milliseconds * @return Response */ public function fetch( @@ -243,6 +245,8 @@ public function fetch( ?array $body = [], ?array $query = [], ?callable $chunks = null, + ?int $timeoutMs = null, + ?int $connectTimeoutMs = null, ): Response { if (!in_array($method, [self::METHOD_PATCH, self::METHOD_GET, self::METHOD_CONNECT, self::METHOD_DELETE, self::METHOD_POST, self::METHOD_HEAD, self::METHOD_OPTIONS, self::METHOD_PUT, self::METHOD_TRACE])) { throw new Exception("Unsupported HTTP method"); @@ -297,8 +301,8 @@ public function fetch( } return strlen($data); }, - CURLOPT_CONNECTTIMEOUT => $this->connectTimeout, - CURLOPT_TIMEOUT => $this->timeout, + CURLOPT_CONNECTTIMEOUT_MS => $connectTimeoutMs ?? $this->connectTimeout, + CURLOPT_TIMEOUT_MS => $timeoutMs ?? $this->timeout, CURLOPT_MAXREDIRS => $this->maxRedirects, CURLOPT_FOLLOWLOCATION => $this->allowRedirects, CURLOPT_USERAGENT => $this->userAgent @@ -339,7 +343,7 @@ public function fetch( } /** - * Get the request timeout. + * Get the request timeout in milliseconds. * * @return int */ @@ -369,7 +373,7 @@ public function getMaxRedirects(): int } /** - * Get the connection timeout. + * Get the connection timeout in milliseconds. * * @return int */