Skip to content

Commit 94ed70c

Browse files
Merge pull request #72 from CodeWithSushil/1.x
## What's new - `curl_close` function is deprecated in PHP 8.5.0 version - use new function `unset()` instead of `curl_close`. - format code base according to PSR-12.
2 parents 43f87d9 + af7d170 commit 94ed70c

File tree

1 file changed

+83
-63
lines changed

1 file changed

+83
-63
lines changed

src/Supabase.php

Lines changed: 83 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,103 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace Supabase\Client;
6+
57
use Exception;
68

79
class Supabase
810
{
9-
private $apikey;
10-
protected $url;
11+
private $apikey;
12+
protected $url;
1113

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+
)
1518
{
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+
}
1924

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+
}
2733
}
28-
}
2934

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+
];
4150

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+
];
5968

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);
6771

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);
7286

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+
}
7596

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+
}
80102
}
81-
curl_close($ch);
82-
}
83103
}

0 commit comments

Comments
 (0)