Skip to content

Commit 4504626

Browse files
v1.0.3
1 parent 8d683f8 commit 4504626

File tree

4 files changed

+96
-246
lines changed

4 files changed

+96
-246
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ cp .env.example .env
3232

3333
```php
3434
<?php
35-
use Supabase\Supabase\Supabase;
35+
use Supabase\Functions;
3636

3737
require_once __DIR__."/vendor/autoload.php";
3838

@@ -45,7 +45,7 @@ $config = array(
4545
'apikey' => $_ENV['SB_APIKEY']
4646
);
4747

48-
$client = new Supabase(
48+
$client = new Functions(
4949
$config['url'],
5050
$config['apikey']
5151
);

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "supabase-php/supabase-client",
33
"description": "Supabase for PHP client.",
44
"keywords": ["supabase","supabase-php","supabase-client"],
5-
"version": "1.0.2",
5+
"version": "1.0.3",
66
"type": "library",
77
"require": {
88
"php": "^7.0||^8.3",

src/Functions.php

Lines changed: 65 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,73 @@
11
<?php
22
namespace Supabase;
3-
43
use Supabase\Supabase\Supabase;
5-
use Dotenv\Dotenv;
64

7-
class Functions extends Supabase{
8-
public $supabase;
9-
10-
public function __construct(){
11-
$dotenv = Dotenv::createImmutable(APP_ROOT);
12-
$dotenv->load();
13-
$config = array(
14-
'url' => $_ENV['SB_URL'],
15-
'apikey' => $_ENV['SB_APIKEY']
16-
);
17-
18-
$client = null;
19-
try{
20-
$this->supabase=$client;
5+
class Functions extends Supabase
6+
{
7+
public function getAllData($table=null)
8+
{
9+
if (!isset($table)) {
10+
echo "Please provide your Supabase table name.";
11+
} else {
12+
$path = "$this->url/rest/v1/$table";
13+
$html = $this->grab($path, "GET");
14+
$data = json_decode($html, true);
15+
return $data;
16+
}
17+
}
18+
19+
public function getSingleData($table=null, $query=[])
20+
{
21+
if (!isset($table)) {
22+
echo "Please provide your Supabase table name.";
23+
} elseif(!isset($query)) {
24+
echo "Please provide your column name.";
25+
} else {
26+
$path = "$this->url/rest/v1/$table?select=$query";
27+
$html = $this->grab($path, "GET");
28+
$data = json_decode($html, true);
29+
return $data;
30+
}
31+
}
32+
33+
public function postData($table=null, $query=[])
34+
{
35+
if (!isset($table)) {
36+
echo "Please provide your Supabase table name.";
37+
} elseif (!isset($query)) {
38+
echo "Please provide your data.";
39+
} else {
40+
$path = "$this->url/rest/v1/$table";
41+
$html = $this->grab($path, "POST", json_encode($query));
42+
return $html;
43+
}
44+
}
45+
46+
public function updateData($table=null, int $id=null, $query=[])
47+
{
48+
if (!isset($table)) {
49+
echo "Please provide your Supabase table name.";
50+
} elseif (!isset($id)) {
51+
echo "Please provide id.";
52+
} elseif (!isset($query)) {
53+
echo "Please provide your data.";
54+
} else {
55+
$path = "$this->url/rest/v1/$table?id=eq.$id";
56+
$html = $this->grab($path, "PATCH", json_encode($query));
57+
return $html;
58+
}
59+
}
2160

22-
$client = new Supabase(
23-
$config['url'],
24-
$config['apikey']
25-
);
26-
} catch(Exception $e){
27-
echo "Error :". $e->getMessage();
61+
public function deleteData($table=null, int $id=null)
62+
{
63+
if (!isset($table)) {
64+
echo "Please provide your Supabase table name.";
65+
} elseif (!isset($id)) {
66+
echo "Please provide id.";
67+
} else {
68+
$path = "$this->url/rest/v1/$table?id=eq.$id";
69+
$html = $this->grab($path, "DELETE");
70+
return $html;
2871
}
2972
}
3073
}

src/Supabase/Supabase.php

Lines changed: 28 additions & 221 deletions
Original file line numberDiff line numberDiff line change
@@ -5,244 +5,51 @@
55
class Supabase
66
{
77
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;
139

14-
public function __construct($url=null, $apikey=null){
10+
public function __construct($url=null, $apikey=null)
11+
{
1512
if (!isset($url)){
1613
throw new Exception("Supabase URL must be specified");
1714
} elseif(!isset($apikey)){
18-
throw new Exception("Supabase API_KEY must be specified", 1);
15+
throw new Exception("Supabase API_KEY must be specified");
1916
} else {
2017
$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;
5919
}
6020
}
61-
62-
public function getAllData($table = null)
21+
22+
public function grab($url, $method, $data=null)
6323
{
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(
18225
"apikey: $this->apikey",
18326
"Authorization: Bearer $this->apikey",
18427
"Content-Type: application/json",
18528
"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+
);
19530

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+
);
19840

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);
20545
}
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;
23548

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;
24652
}
53+
curl_close($ch);
24754
}
24855
}

0 commit comments

Comments
 (0)