<?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);
$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'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

$result = json_decode(curl_exec($ch), true);
$token = $result['data']['token'];

$payload3 = json_encode([
    'act' => 'GetSemester',
    'token' => $token,
    'filter' => "a_periode_aktif = '1'"
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload3);
$res3 = curl_exec($ch);
echo "GetSemester Aktif:\n$res3\n\n";

$payload4 = json_encode([
    'act' => 'GetDictionary',
    'token' => $token
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload4);
$res4 = curl_exec($ch);
$dict = json_decode($res4, true);
echo "Dict items matching 'periode' or 'semester':\n";
foreach($dict['data'] as $d) {
    if(stripos($d['nama_fungsi'], 'periode') !== false || stripos($d['nama_fungsi'], 'semester') !== false) {
        echo $d['nama_fungsi'] . "\n";
    }
}

curl_close($ch);
?>
