<?php
require_once "../mainconfig.php"; //koneksi kepada database
mysqli_query($db, "TRUNCATE TABLE service_cat");
$key = "aa9f8e-bb2b3f-61fa01-587b63-188230"; // your api key
$postdata = "api_key=$key&api_id=2068";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://irvankedesmm.co.id/api/services");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$chresult = curl_exec($ch);
//echo $chresult;
curl_close($ch);
$json_result = json_decode($chresult, true);
$indeks = 0;
// get data service
while ($indeks < count($json_result["data"])) {
    $category = $json_result["data"][$indeks]["category"];
    $category2 = str_replace("(", "[", $category);
    $category2 = str_replace(")", "]", $category2);
    $category2 = mysqli_real_escape_string($db, $category2);
    //echo $category2 . "<br>";
    $indeks++;

    $check_category = mysqli_query($db,"SELECT * FROM service_cat WHERE name = '$category2' AND code = '$category2'");
    if (mysqli_num_rows($check_category) > 0) {
        //echo "Service Sudah Ada Di database => $category2  \n <br />";
    } else {
        $insert2 = mysqli_query($db,"INSERT INTO service_cat (name, code) VALUES ('$category2','$category2')"); //Memasukan Kepada Database (OPTIONAL)
    }
}
$update_monitor = mysqli_query($db,"UPDATE monitor_cron SET status = 'Success', response = 'File Get category berhasil dijalankan', tanggal = '$date $time' WHERE judul = 'get_category'");
echo "File Get category berhasil dijalankan";
?>
