Lädt...

🔧 Import json to Supabase dengan PHP command line


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Image description

<?php
// Supabase API Config
$supabaseUrl = 'https://wekekekekeke.supabase.co';
$supabaseKey = 'endaseendasmuendiasmu';

$table = "bencana";

// Meminta input tahun dari user
if (PHP_SAPI === 'cli') {
    echo "Masukkan tahun: ";
    $tahun = trim(fgets(STDIN));
} else {
    $tahun = isset($_GET['tahun']) ? $_GET['tahun'] : date('Y');
}

$filename = "$tahun.json";

// Periksa apakah file JSON ada
if (!file_exists($filename)) {
    die("File $filename tidak ditemukan.\n");
}

// Membaca file JSON
$jsonString = file_get_contents($filename);
$dataArray = json_decode($jsonString, true);

// Pastikan JSON memiliki fitur yang diambil
if (isset($dataArray['features']) && is_array($dataArray['features'])) {
    $allData = [];

    // Loop melalui semua features dan ambil properties-nya
    foreach ($dataArray['features'] as $feature) {
        if (isset($feature['properties'])) {
            $allData[] = $feature['properties'];
        }
    }

    if (!empty($allData)) {
        $batchSize = 100; // Jumlah data per batch
        $totalBatches = ceil(count($allData) / $batchSize); // Hitung total batch

        for ($i = 0; $i < $totalBatches; $i++) {
            // Ambil 100 data per batch
            $batchData = array_slice($allData, $i * $batchSize, $batchSize);

            // Konversi ke JSON untuk dikirim ke Supabase
            $postData = json_encode($batchData);

            // Kirim batch ke Supabase
            $ch = curl_init("$supabaseUrl/rest/v1/$table");
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, [
                "Content-Type: application/json",
                "Authorization: Bearer $supabaseKey",
                "apikey: $supabaseKey",
                "Prefer: resolution=merge-duplicates" // Hindari duplikasi data
            ]);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

            $response = curl_exec($ch);
            curl_close($ch);

            echo "Response: $response \n";
            echo "[".$tahun."] Batch " . ($i + 1) . " dari $totalBatches selesai.\n";
            flush();
        }
        echo "Semua batch telah dikirim ke Supabase.\n";
    } else {
        echo "Tidak ada data yang bisa dimasukkan ke dalam database. \n";
    }
} else {
    echo "Data tidak ditemukan dalam JSON. \n";
}
?>

Tampilannya seperti ini, tinggal masukkan tahun/nama file json dan akan diproses per batch isi 100.
kalau selesai atau error ada peringatannya.

Image description

...

🔧 Import json to Supabase dengan PHP command line


📈 69.74 Punkte
🔧 Programmierung

🔧 Scrape peraturan BPK dengan PHP, DiDOM dan Supabase


📈 37.26 Punkte
🔧 Programmierung

🔧 Comparing JSON Libraries in .NET: Newtonsoft.Json vs. System.Text.Json


📈 28.56 Punkte
🔧 Programmierung

🔧 How to setup Supabase with Nextjs for authentication | Supabase Auth


📈 28.26 Punkte
🔧 Programmierung

🔧 How to setup Supabase with Nextjs for authentication | Supabase Auth


📈 28.26 Punkte
🔧 Programmierung

🔧 Setup User Auth for your Reflex Python app using supabase (supabase-py)


📈 28.26 Punkte
🔧 Programmierung

🕵️ Cisco IOS XR Command Line line os command injection


📈 25.62 Punkte
🕵️ Sicherheitslücken

🕵️ IBM Domino 9.0/9.0.1 Command Line nsd.exe Command Line Argument privilege escalation


📈 25.62 Punkte
🕵️ Sicherheitslücken

🔧 Need a JSON object comparison that's better than line-by-line


📈 25.54 Punkte
🔧 Programmierung

🔧 Crossing the Line before the Finish Line. Also the line before that.


📈 24.02 Punkte
🔧 Programmierung

🕵️ PlaySMS 1.4 Phonebook Import import.php User-Agent privilege escalation


📈 23.15 Punkte
🕵️ Sicherheitslücken

🕵️ PlaySMS 1.4 Phonebook Import import.php HTTP Header erweiterte Rechte


📈 23.15 Punkte
🕵️ Sicherheitslücken

🔧 Menyederhanakan 2000 koordinat menjadi 40 koordninat dengan PHP


📈 23.13 Punkte
🔧 Programmierung

🔧 R2 Cloudflare penyimpanan object dengan PHP


📈 23.13 Punkte
🔧 Programmierung

🔧 How to Easily Import Large SQL Database Files into MySQL Using Command Line


📈 22.96 Punkte
🔧 Programmierung

🕵️ Avideo up to 8.8 import.json.php access control


📈 22.52 Punkte
🕵️ Sicherheitslücken

🐧 Practical JSON at the Command Line using Jello


📈 22.33 Punkte
🐧 Linux Tipps

🐧 Jello v1.3 now supports dot notation (Command-line JSON filter using python syntax)


📈 22.33 Punkte
🐧 Linux Tipps

🐧 Manipulate JSON in command line thanks to JQ example with cURL


📈 22.33 Punkte
🐧 Linux Tipps

🐧 A general request for command line utils: JSON


📈 22.33 Punkte
🐧 Linux Tipps

🔧 Validating JSON with JSON Schema and PHP


📈 21.89 Punkte
🔧 Programmierung

🐧 Weekly Command: comparing files line by line with diff


📈 20.82 Punkte
🐧 Linux Tipps

matomo