<?php
$mysqli = new mysqli("127.0.0.1", "root", "", "siakad");
$res = $mysqli->query("SELECT * FROM pengaturan");
$settings = [];
while($row = $res->fetch_assoc()) {
    $settings[$row['pengaturan_key']] = $row['pengaturan_value'];
}

$url = $settings['pddikti_url'];
$username = $settings['pddikti_username'];
$password = $settings['pddikti_password'];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$payload = json_encode(['act' => 'GetToken', 'username' => $username, 'password' => $password]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

$resRaw = curl_exec($ch);
$result = json_decode($resRaw, true);

if (!isset($result['data']['token'])) {
    die("Failed to get token");
}

$token = $result['data']['token'];

// Check dictionary
$payload2 = json_encode([
    'act' => 'GetDictionary',
    'token' => $token,
    'fungsi' => 'InsertBiodataMahasiswa'
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload2);
$dictRaw = curl_exec($ch);

echo "InsertBiodataMahasiswa Dictionary:\n";
echo $dictRaw;
echo "\n\n";

$payload3 = json_encode([
    'act' => 'GetDictionary',
    'token' => $token,
    'fungsi' => 'InsertRiwayatPendidikanMahasiswa'
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload3);
$dictRaw2 = curl_exec($ch);

echo "InsertRiwayatPendidikanMahasiswa Dictionary:\n";
echo $dictRaw2;
echo "\n\n";

?>
