|
5 | 5 | class Supabase |
6 | 6 | { |
7 | 7 | private string|null $apikey; |
8 | | - private string|null $project_id; |
9 | | - protected string|null $table = null; |
10 | | - protected $conn; |
11 | | - protected $url; |
12 | | - public $data; |
| 8 | + protected string|null $url; |
13 | 9 |
|
14 | | - public function __construct($url=null, $apikey=null){ |
| 10 | + public function __construct($url=null, $apikey=null) |
| 11 | + { |
15 | 12 | if (!isset($url)){ |
16 | 13 | throw new Exception("Supabase URL must be specified"); |
17 | 14 | } elseif(!isset($apikey)){ |
18 | | - throw new Exception("Supabase API_KEY must be specified", 1); |
| 15 | + throw new Exception("Supabase API_KEY must be specified"); |
19 | 16 | } else { |
20 | 17 | $this->apikey = $apikey; |
21 | | - $this->project_id = $url; |
22 | | - } |
23 | | - |
24 | | - $URL = "$this->project_id/rest/v1/$this->table"; |
25 | | - $this->url = $URL; |
26 | | - |
27 | | - $headers = [ |
28 | | - "apikey: $this->apikey", |
29 | | - "Authorization: Bearer $this->apikey", |
30 | | - "Content-Type: application/json", |
31 | | - ]; |
32 | | - |
33 | | - // CURL |
34 | | - $ch = null; |
35 | | - $this->conn = $ch; |
36 | | - $this->conn = curl_init($url); |
37 | | - |
38 | | - $options = [ |
39 | | - CURLOPT_RETURNTRANSFER => true, |
40 | | - CURLOPT_TIMEOUT => 120, |
41 | | - CURLOPT_ENCODING => "UTF-8", |
42 | | - CURLOPT_HTTPHEADER => $headers, |
43 | | - CURLOPT_CONNECTTIMEOUT => 120, |
44 | | - ]; |
45 | | - |
46 | | - curl_setopt_array($this->conn, $options); |
47 | | - |
48 | | - $result = curl_exec($this->conn); |
49 | | - $data = json_decode($result, true); |
50 | | - |
51 | | - if (curl_errno($this->conn)) { |
52 | | - $err = curl_error($this->conn); |
53 | | - echo "Error" . $err; |
54 | | - curl_close($this->conn); |
55 | | - } else { |
56 | | - if (isset($data["message"])) { |
57 | | - echo $data["message"] . "<br/>"; |
58 | | - } |
| 18 | + $this->url = $url; |
59 | 19 | } |
60 | 20 | } |
61 | | - |
62 | | - public function getAllData($table = null) |
| 21 | + |
| 22 | + public function grab($url, $method, $data=null) |
63 | 23 | { |
64 | | - if (!isset($table)) { |
65 | | - echo "Please provide Supabase table name"; |
66 | | - } else { |
67 | | - //$this->url=$this->url."?select=$query"; |
68 | | - $this->table = $table; |
69 | | - $URL="$this->project_id/rest/v1/$this->table"; |
70 | | - $this->url = $URL; |
71 | | - |
72 | | - $header = [ |
73 | | - "apikey: $this->apikey", |
74 | | - "Authorization: Bearer $this->apikey", |
75 | | - "Content-Type: application/json", |
76 | | - ]; |
77 | | - |
78 | | - $this->conn = curl_init(); |
79 | | - curl_setopt($this->conn, CURLOPT_URL, $this->url); |
80 | | - curl_setopt($this->conn, CURLOPT_RETURNTRANSFER, true); |
81 | | - curl_setopt($this->conn, CURLOPT_HTTPHEADER, $header); |
82 | | - $result = curl_exec($this->conn); |
83 | | - $data = json_decode($result, true); |
84 | | - |
85 | | - if (curl_errno($this->conn)) { |
86 | | - echo "Error: ". curl_error($this->conn); |
87 | | - curl_close($this->conn); |
88 | | - } else { |
89 | | - return $data; |
90 | | - } |
91 | | - } |
92 | | - } |
93 | | - |
94 | | - public function getSingleData($table = null, $query = []) |
95 | | - { |
96 | | - if (!isset($table)) { |
97 | | - echo "Please provide table name."; |
98 | | - } else { |
99 | | - |
100 | | - $this->table = $table; |
101 | | - $URL="$this->project_id/rest/v1/$this->table"; |
102 | | - $this->url = $URL; |
103 | | - |
104 | | - $this->url=$this->url."?select=$query"; |
105 | | - $header = [ |
106 | | - "apikey: $this->apikey", |
107 | | - "Authorization: Bearer $this->apikey", |
108 | | - "Content-Type: application/json", |
109 | | - ]; |
110 | | - |
111 | | - $this->conn = curl_init(); |
112 | | - curl_setopt($this->conn, CURLOPT_URL, $this->url); |
113 | | - curl_setopt($this->conn, CURLOPT_RETURNTRANSFER, true); |
114 | | - curl_setopt($this->conn, CURLOPT_HTTPHEADER, $header); |
115 | | - $result = curl_exec($this->conn); |
116 | | - $data = json_decode($result, true); |
117 | | - if (curl_errno($this->conn)) { |
118 | | - echo "Error: " . curl_error($this->conn); |
119 | | - curl_close($this->conn); |
120 | | - } else { |
121 | | - return $data; |
122 | | - } |
123 | | - } |
124 | | - } |
125 | | - |
126 | | - public function postData($table = null, $query = []) |
127 | | - { |
128 | | - if (!isset($table)) { |
129 | | - echo "Please provide your Supabase table name."; |
130 | | - } elseif (!isset($query)) { |
131 | | - echo "Please insert your data."; |
132 | | - } else { |
133 | | - $this->table = $table; |
134 | | - $URL = "$this->project_id/rest/v1/$this->table"; |
135 | | - $this->url = $URL; |
136 | | - |
137 | | - // Set up the cURL request |
138 | | - $this->conn = curl_init(); |
139 | | - curl_setopt($this->conn, CURLOPT_URL, $this->url); |
140 | | - curl_setopt($this->conn, CURLOPT_RETURNTRANSFER, true); |
141 | | - curl_setopt($this->conn, CURLOPT_POST, true); |
142 | | - curl_setopt($this->conn, CURLOPT_POSTFIELDS, json_encode($query)); |
143 | | - curl_setopt($this->conn, CURLOPT_HTTPHEADER, [ |
144 | | - "apikey: $this->apikey", |
145 | | - "Authorization: Bearer $this->apikey", |
146 | | - "Content-Type: application/json", |
147 | | - "Prefer: return=minimal", |
148 | | - ]); |
149 | | - curl_setopt($this->conn, CURLOPT_TIMEOUT, 120); |
150 | | - // Execute the request and capture the response |
151 | | - $response = curl_exec($this->conn); |
152 | | - $result = json_decode($response, true); |
153 | | - // Check for cURL errors |
154 | | - if (curl_errno($this->conn)) { |
155 | | - echo "Error:" . curl_error($this->conn); |
156 | | - curl_close($this->conn); |
157 | | - } else { |
158 | | - // Output the response |
159 | | - echo "Data Post successfully"; |
160 | | - } |
161 | | - // Close the cURL session |
162 | | - curl_close($this->conn); |
163 | | - } |
164 | | - } |
165 | | - |
166 | | - public function updateData($table = null, int $id = null, $query = []) |
167 | | - { |
168 | | - if (!isset($table)) { |
169 | | - echo "Please provide your Supabase table name."; |
170 | | - } elseif (!isset($query)) { |
171 | | - echo "Please provide your data."; |
172 | | - } elseif (!isset($id)) { |
173 | | - echo "Please provide id number."; |
174 | | - } else { |
175 | | - $this->table = $table; |
176 | | - $URL="$this->project_id/rest/v1/$this->table"; |
177 | | - $this->url = $URL; |
178 | | - $this->url = $this->url."?id=eq.$id"; |
179 | | - } |
180 | | - |
181 | | - $headers = [ |
| 24 | + $headers = array( |
182 | 25 | "apikey: $this->apikey", |
183 | 26 | "Authorization: Bearer $this->apikey", |
184 | 27 | "Content-Type: application/json", |
185 | 28 | "Prefer: return=minimal", |
186 | | - ]; |
187 | | - |
188 | | - $this->conn = curl_init(); |
189 | | - curl_setopt($this->conn, CURLOPT_URL, $this->url); |
190 | | - curl_setopt($this->conn, CURLOPT_RETURNTRANSFER, true); |
191 | | - curl_setopt($this->conn, CURLOPT_CUSTOMREQUEST, "PATCH"); |
192 | | - curl_setopt($this->conn, CURLOPT_POSTFIELDS, json_encode($query)); |
193 | | - curl_setopt($this->conn, CURLOPT_HTTPHEADER, $headers); |
194 | | - curl_setopt($this->conn, CURLOPT_TIMEOUT, 120); |
| 29 | + ); |
195 | 30 |
|
196 | | - $response = curl_exec($this->conn); |
197 | | - $result = json_decode($response, true); |
| 31 | + $options = array( |
| 32 | + CURLOPT_URL => $url, |
| 33 | + CURLOPT_RETURNTRANSFER => true, |
| 34 | + CURLOPT_HTTPHEADER => $headers, |
| 35 | + CURLOPT_CUSTOMREQUEST => $method, |
| 36 | + CURLOPT_TIMEOUT => 120, |
| 37 | + CURLOPT_ENCODING => "UTF-8", |
| 38 | + CURLOPT_CONNECTTIMEOUT => 120 |
| 39 | + ); |
198 | 40 |
|
199 | | - if (curl_errno($this->conn)) { |
200 | | - echo "Error: " . curl_error($this->conn); |
201 | | - curl_close($this->conn); |
202 | | - } else { |
203 | | - echo "Data updated successfully."; |
204 | | - return $result; |
| 41 | + $ch = curl_init(); |
| 42 | + curl_setopt_array($ch, $options); |
| 43 | + if(isset($data)){ |
| 44 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $data); |
205 | 45 | } |
206 | | - curl_close($this->conn); |
207 | | - } |
208 | | - |
209 | | - public function deleteData($table = null, int $id = null) |
210 | | - { |
211 | | - if (!isset($table)) { |
212 | | - echo "Please provide your Supabase table name."; |
213 | | - } elseif (!isset($id)) { |
214 | | - echo "Please provide id."; |
215 | | - } else { |
216 | | - $this->table = $table; |
217 | | - $URL = "$this->project_id/rest/v1/$this->table"; |
218 | | - $this->url = $URL; |
219 | | - |
220 | | - $this->url = $this->url."?id=eq.$id"; |
221 | | - |
222 | | - $headers = [ |
223 | | - "apikey: $this->apikey", |
224 | | - "Authorization: Bearer $this->apikey", |
225 | | - "Content-Type: application/json", |
226 | | - ]; |
227 | | - |
228 | | - $this->conn = curl_init(); |
229 | | - curl_setopt($this->conn, CURLOPT_URL, $this->url); |
230 | | - curl_setopt($this->conn, CURLOPT_RETURNTRANSFER, true); |
231 | | - curl_setopt($this->conn, CURLOPT_HTTPHEADER, $headers); |
232 | | - curl_setopt($this->conn, CURLOPT_CUSTOMREQUEST, "DELETE"); |
233 | | - curl_setopt($this->conn, CURLOPT_POSTFIELDS, json_encode($id)); |
234 | | - curl_setopt($this->conn, CURLOPT_TIMEOUT, 120); |
| 46 | + $html = curl_exec($ch); |
| 47 | + return $html; |
235 | 48 |
|
236 | | - $response = curl_exec($this->conn); |
237 | | - $result = json_decode($response, true); |
238 | | - if (curl_errno($this->conn)) { |
239 | | - echo "Error: " . curl_error($this->conn); |
240 | | - curl_close($this->conn); |
241 | | - } else { |
242 | | - echo "Your data delete successfully."; |
243 | | - return $result; |
244 | | - } |
245 | | - curl_close($this->conn); |
| 49 | + if(curl_errno($ch)){ |
| 50 | + $error = curl_error($ch); |
| 51 | + echo "Error :". $error; |
246 | 52 | } |
| 53 | + curl_close($ch); |
247 | 54 | } |
248 | 55 | } |
0 commit comments