Spaces:
Sleeping

File size: 1,564 Bytes
9694b21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
// /api/services/SupabaseService.php

class SupabaseService {
    private string $url;
    private string $serviceKey;

    public function __construct(string $url, string $serviceKey) {
        $this->url = $url;
        $this->serviceKey = $serviceKey;
    }

    /**
     * حفظ بيانات الإضافة في Supabase.
     */
    public function saveAddon(array $addonData): array {
        $url = $this->url . '/rest/v1/addons';
        
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($addonData));
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'apikey: ' . $this->serviceKey,
            'Authorization: Bearer ' . $this->serviceKey,
            'Content-Type: application/json',
            'Prefer: return=representation'
        ]);

        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        if ($httpCode >= 400) {
            $errorData = json_decode($response, true);
            $errorMessage = $errorData['message'] ?? $errorData['error'] ?? $response;
            throw new Exception("فشل حفظ البيانات في Supabase: " . $errorMessage);
        }

        $data = json_decode($response, true);
        if (json_last_error() !== JSON_ERROR_NONE) {
            throw new Exception("فشل في تحليل استجابة Supabase: " . json_last_error_msg());
        }

        return $data;
    }
}