11<?php
2+ declare (strict_types=1 );
3+
24namespace Supabase ;
35use Exception ;
46
57class Supabase
68{
7- private string | null $ apikey ;
8- protected string | null $ url ;
9+ private $ apikey ;
10+ protected $ url ;
911
10- public function __construct ($ url =null , $ apikey =null )
12+ public function __construct (? string $ url =null , ? string $ apikey =null )
1113 {
1214 if (!isset ($ url )){
1315 throw new Exception ("Supabase URL must be specified " );
@@ -24,10 +26,12 @@ protected function grab($url, $method, $data=null)
2426 $ headers = array (
2527 "apikey: $ this ->apikey " ,
2628 "Authorization: Bearer $ this ->apikey " ,
27- "Content-Type: application/json " ,
28- "Prefer: return=minimal " ,
29- "Range: 0-9 " ,
30- "Prefer: resolution=merge-duplicates "
29+ 'Content-Type: application/json ' ,
30+ 'Accept: application/json ' ,
31+ 'Content-Type: application/x-www-form-urlencoded ' ,
32+ 'Prefer: return=minimal ' ,
33+ 'Range: 0-9 ' ,
34+ 'Prefer: resolution=merge-duplicates '
3135 );
3236
3337 $ options = array (
@@ -37,9 +41,11 @@ protected function grab($url, $method, $data=null)
3741 CURLOPT_SSL_VERIFYHOST => 2 ,
3842 CURLOPT_HTTPHEADER => $ headers ,
3943 CURLOPT_CUSTOMREQUEST => $ method ,
40- CURLOPT_TIMEOUT => 120 ,
41- CURLOPT_ENCODING => "UTF-8 " ,
42- CURLOPT_CONNECTTIMEOUT => 120
44+ CURLOPT_TIMEOUT => 30 ,
45+ CURLOPT_ENCODING => "" ,
46+ CURLOPT_CONNECTTIMEOUT => 10 ,
47+ CURLOPT_FRESH_CONNECT => true ,
48+ CURLOPT_FORBID_REUSE => true
4349 );
4450
4551 $ ch = curl_init ();
@@ -52,7 +58,16 @@ protected function grab($url, $method, $data=null)
5258
5359 if (curl_errno ($ ch )){
5460 $ error = curl_error ($ ch );
55- echo "Error : " . $ error ;
61+ echo json_encode ($ error , JSON_PRETTY_PRINT );
62+ }
63+
64+ // Validate HTTP status code
65+ $ http_code = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
66+
67+ if ($ http_code !== 200 ) {
68+ echo "Request failed with status code $ http_code " ;
69+ curl_close ($ ch );
70+ exit ;
5671 }
5772 curl_close ($ ch );
5873 }
0 commit comments