$maxBytes) { throw new \RuntimeException('Remote response exceeds verification size.'); }if (microtime(true) >= $deadline) { throw new \RuntimeException('Remote transfer budget expired.'); } }; $stream = Utils::streamFor(fopen('php://temp', 'w+b')); $bytes = 0; $sink = FnStream::decorate($stream, ['write' => function (string $chunk) use ($stream, &$bytes, $maxBytes, $deadline): int { // Returning zero makes cURL abort immediately; throwing from its progress // callback can defer the exception until the entire body was buffered. if ($bytes + strlen($chunk) > $maxBytes || microtime(true) >= $deadline) { return 0; } $bytes += strlen($chunk); return $stream->write($chunk); }]); return ['sink' => $sink, 'timeout' => $remaining, 'connect_timeout' => min(10, $remaining), 'decode_content' => false, 'on_headers' => function (ResponseInterface $response) use ($guard): void { $guard((float) $response->getHeaderLine('Content-Length')); }]; } }