|
1 | 1 | <?php |
| 2 | + |
2 | 3 | declare(strict_types=1); |
3 | 4 |
|
4 | 5 | namespace Supabase\Client; |
| 6 | + |
5 | 7 | use Exception; |
6 | 8 |
|
7 | 9 | class Supabase |
8 | 10 | { |
9 | | - private $apikey; |
10 | | - protected $url; |
| 11 | + private $apikey; |
| 12 | + protected $url; |
11 | 13 |
|
12 | | - public function __construct(?string $url=null, ?string $apikey=null) |
13 | | - { |
14 | | - if(!filter_var($url, FILTER_VALIDATE_URL)) |
| 14 | + public function __construct( |
| 15 | + ?string $url=null, |
| 16 | + ?string $apikey=null |
| 17 | + ) |
15 | 18 | { |
16 | | - echo "Invalid URL format"; |
17 | | - exit(); |
18 | | - } |
| 19 | + if(!filter_var($url, FILTER_VALIDATE_URL)) |
| 20 | + { |
| 21 | + echo "Invalid URL format"; |
| 22 | + exit(); |
| 23 | + } |
19 | 24 |
|
20 | | - if (!isset($url)){ |
21 | | - throw new Exception("Supabase URL must be specified"); |
22 | | - } elseif(!isset($apikey)){ |
23 | | - throw new Exception("Supabase API_KEY must be specified"); |
24 | | - } else { |
25 | | - $this->apikey = $apikey; |
26 | | - $this->url = $url ."/rest/v1"; |
| 25 | + if (!isset($url)){ |
| 26 | + throw new Exception("Supabase URL must be specified"); |
| 27 | + } elseif(!isset($apikey)){ |
| 28 | + throw new Exception("Supabase API_KEY must be specified"); |
| 29 | + } else { |
| 30 | + $this->apikey = $apikey; |
| 31 | + $this->url = $url ."/rest/v1"; |
| 32 | + } |
27 | 33 | } |
28 | | - } |
29 | 34 |
|
30 | | - protected function grab(string $url, string $method, ?string $data=null) |
31 | | - { |
32 | | - $headers = array( |
33 | | - "apikey: $this->apikey", |
34 | | - "Authorization: Bearer $this->apikey", |
35 | | - 'Content-Type: application/json', |
36 | | - 'Accept: application/json', |
37 | | - 'Prefer: return=minimal', |
38 | | - 'Range: 0-9', |
39 | | - 'Prefer: resolution=merge-duplicates' |
40 | | - ); |
| 35 | + protected function grab( |
| 36 | + string $url, |
| 37 | + string $method, |
| 38 | + ?string $data=null |
| 39 | + ) |
| 40 | + { |
| 41 | + $headers = [ |
| 42 | + "apikey: $this->apikey", |
| 43 | + "Authorization: Bearer $this->apikey", |
| 44 | + 'Content-Type: application/json', |
| 45 | + 'Accept: application/json', |
| 46 | + 'Prefer: return=minimal', |
| 47 | + 'Range: 0-9', |
| 48 | + 'Prefer: resolution=merge-duplicates' |
| 49 | + ]; |
41 | 50 |
|
42 | | - $options = array( |
43 | | - CURLOPT_URL => $url, |
44 | | - CURLOPT_NOBODY => false, |
45 | | - CURLOPT_RETURNTRANSFER => true, |
46 | | - CURLOPT_SSL_VERIFYPEER => true, |
47 | | - CURLOPT_SSL_VERIFYHOST => 2, |
48 | | - CURLOPT_SSL_OPTIONS => CURLSSLOPT_NATIVE_CA, |
49 | | - CURLOPT_FOLLOWLOCATION => true, |
50 | | - CURLOPT_HTTPHEADER => $headers, |
51 | | - CURLOPT_CUSTOMREQUEST => $method, |
52 | | - CURLOPT_MAXREDIRS => 10, |
53 | | - CURLOPT_TIMEOUT => 30, |
54 | | - CURLOPT_ENCODING => "", |
55 | | - CURLOPT_CONNECTTIMEOUT => 10, |
56 | | - CURLOPT_FRESH_CONNECT => true, |
57 | | - CURLOPT_FORBID_REUSE => true |
58 | | - ); |
| 51 | + $options = [ |
| 52 | + CURLOPT_URL => $url, |
| 53 | + CURLOPT_NOBODY => false, |
| 54 | + CURLOPT_RETURNTRANSFER => true, |
| 55 | + CURLOPT_SSL_VERIFYPEER => true, |
| 56 | + CURLOPT_SSL_VERIFYHOST => 2, |
| 57 | + CURLOPT_SSL_OPTIONS => CURLSSLOPT_NATIVE_CA, |
| 58 | + CURLOPT_FOLLOWLOCATION => true, |
| 59 | + CURLOPT_HTTPHEADER => $headers, |
| 60 | + CURLOPT_CUSTOMREQUEST => $method, |
| 61 | + CURLOPT_MAXREDIRS => 10, |
| 62 | + CURLOPT_TIMEOUT => 30, |
| 63 | + CURLOPT_ENCODING => "", |
| 64 | + CURLOPT_CONNECTTIMEOUT => 10, |
| 65 | + CURLOPT_FRESH_CONNECT => true, |
| 66 | + CURLOPT_FORBID_REUSE => true |
| 67 | + ]; |
59 | 68 |
|
60 | | - $ch = curl_init(); |
61 | | - curl_setopt_array($ch, $options); |
62 | | - if(isset($data)){ |
63 | | - curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); |
64 | | - } |
65 | | - $html = curl_exec($ch); |
66 | | - return $html; |
| 69 | + $ch = curl_init(); |
| 70 | + curl_setopt_array($ch, $options); |
67 | 71 |
|
68 | | - if(curl_errno($ch)){ |
69 | | - $error = curl_error($ch); |
70 | | - echo json_encode($error, JSON_PRETTY_PRINT); |
71 | | - } |
| 72 | + if(isset($data)){ |
| 73 | + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); |
| 74 | + } |
| 75 | + |
| 76 | + $html = curl_exec($ch); |
| 77 | + return $html; |
| 78 | + |
| 79 | + if(curl_errno($ch)){ |
| 80 | + $error = curl_error($ch); |
| 81 | + echo json_encode($error, JSON_PRETTY_PRINT); |
| 82 | + } |
| 83 | + |
| 84 | + // Validate HTTP status code |
| 85 | + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
72 | 86 |
|
73 | | - // Validate HTTP status code |
74 | | - $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 87 | + if ($http_code !== 200) { |
| 88 | + echo "Request failed with status code $http_code"; |
| 89 | + if(PHP_VERSION_ID >= 80000){ |
| 90 | + unset($ch); |
| 91 | + } else { |
| 92 | + curl_close($ch); |
| 93 | + exit; |
| 94 | + } |
| 95 | + } |
75 | 96 |
|
76 | | - if ($http_code !== 200) { |
77 | | - echo "Request failed with status code $http_code"; |
78 | | - curl_close($ch); |
79 | | - exit; |
| 97 | + if(PHP_VERSION_ID >= 80000){ |
| 98 | + unset($ch); |
| 99 | + } else { |
| 100 | + curl_close($ch); |
| 101 | + } |
80 | 102 | } |
81 | | - curl_close($ch); |
82 | | - } |
83 | 103 | } |
0 commit comments