|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Supabase\Client\Http; |
| 6 | + |
| 7 | +use JsonException; |
| 8 | +use RuntimeException; |
| 9 | + |
| 10 | +class CurlClient |
| 11 | +{ |
| 12 | + public function send( |
| 13 | + string $method, |
| 14 | + string $url, |
| 15 | + array $headers = [], |
| 16 | + array|string|null $body = null |
| 17 | + ): Response { |
| 18 | + $curl = curl_init(); |
| 19 | + |
| 20 | + if ($curl === false) { |
| 21 | + throw new RuntimeException('Unable to initialize cURL.'); |
| 22 | + } |
| 23 | + |
| 24 | + $formattedHeaders = []; |
| 25 | + |
| 26 | + foreach ($headers as $key => $value) { |
| 27 | + $formattedHeaders[] = "{$key}: {$value}"; |
| 28 | + } |
| 29 | + |
| 30 | + $payload = null; |
| 31 | + |
| 32 | + if ($body !== null) { |
| 33 | + $payload = is_array($body) |
| 34 | + ? json_encode($body, JSON_THROW_ON_ERROR) |
| 35 | + : $body; |
| 36 | + } |
| 37 | + |
| 38 | + curl_setopt_array($curl, [ |
| 39 | + CURLOPT_URL => $url, |
| 40 | + |
| 41 | + CURLOPT_CUSTOMREQUEST => strtoupper($method), |
| 42 | + |
| 43 | + CURLOPT_RETURNTRANSFER => true, |
| 44 | + |
| 45 | + CURLOPT_HTTPHEADER => $formattedHeaders, |
| 46 | + |
| 47 | + CURLOPT_POSTFIELDS => $payload, |
| 48 | + |
| 49 | + /* |
| 50 | + |-------------------------------------------------------------------------- |
| 51 | + | Security |
| 52 | + |-------------------------------------------------------------------------- |
| 53 | + */ |
| 54 | + |
| 55 | + CURLOPT_SSL_VERIFYPEER => true, |
| 56 | + CURLOPT_SSL_VERIFYHOST => 2, |
| 57 | + |
| 58 | + CURLOPT_PROTOCOLS => CURLPROTO_HTTPS, |
| 59 | + |
| 60 | + CURLOPT_REDIR_PROTOCOLS => CURLPROTO_HTTPS, |
| 61 | + |
| 62 | + CURLOPT_FOLLOWLOCATION => false, |
| 63 | + |
| 64 | + /* |
| 65 | + |-------------------------------------------------------------------------- |
| 66 | + | Performance |
| 67 | + |-------------------------------------------------------------------------- |
| 68 | + */ |
| 69 | + |
| 70 | + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2TLS, |
| 71 | + |
| 72 | + CURLOPT_TCP_KEEPALIVE => 1, |
| 73 | + |
| 74 | + CURLOPT_TCP_KEEPIDLE => 120, |
| 75 | + |
| 76 | + CURLOPT_TCP_KEEPINTVL => 60, |
| 77 | + |
| 78 | + CURLOPT_ENCODING => '', |
| 79 | + |
| 80 | + CURLOPT_FORBID_REUSE => false, |
| 81 | + |
| 82 | + CURLOPT_FRESH_CONNECT => false, |
| 83 | + |
| 84 | + /* |
| 85 | + |-------------------------------------------------------------------------- |
| 86 | + | Timeouts |
| 87 | + |-------------------------------------------------------------------------- |
| 88 | + */ |
| 89 | + |
| 90 | + CURLOPT_CONNECTTIMEOUT => 10, |
| 91 | + |
| 92 | + CURLOPT_TIMEOUT => 30, |
| 93 | + |
| 94 | + /* |
| 95 | + |-------------------------------------------------------------------------- |
| 96 | + | Safety |
| 97 | + |-------------------------------------------------------------------------- |
| 98 | + */ |
| 99 | + |
| 100 | + CURLOPT_MAXREDIRS => 0, |
| 101 | + |
| 102 | + CURLOPT_FAILONERROR => false, |
| 103 | + |
| 104 | + CURLOPT_HEADER => false, |
| 105 | + ]); |
| 106 | + |
| 107 | + $responseBody = curl_exec($curl); |
| 108 | + |
| 109 | + if ($responseBody === false) { |
| 110 | + $error = curl_error($curl); |
| 111 | + |
| 112 | + curl_close($curl); |
| 113 | + |
| 114 | + throw new RuntimeException( |
| 115 | + message: $error |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + $statusCode = curl_getinfo( |
| 120 | + $curl, |
| 121 | + CURLINFO_HTTP_CODE |
| 122 | + ); |
| 123 | + |
| 124 | + curl_close($curl); |
| 125 | + |
| 126 | + return new Response( |
| 127 | + body: $responseBody, |
| 128 | + status: $statusCode |
| 129 | + ); |
| 130 | + } |
| 131 | +} |
0 commit comments