<?php
$db = new PDO('mysql:host=localhost;dbname=siakad', 'root', '');
$stmt = $db->query("SELECT pengaturan_value FROM pengaturan WHERE pengaturan_key='pddikti_url'");
$url = $stmt->fetchColumn();
$stmt = $db->query("SELECT pengaturan_value FROM pengaturan WHERE pengaturan_key='pddikti_username'");
$username = $stmt->fetchColumn();
$stmt = $db->query("SELECT pengaturan_value FROM pengaturan WHERE pengaturan_key='pddikti_password'");
$password = $stmt->fetchColumn();

echo "URL: $url\nUser: $username\n";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['act' => 'GetToken', 'username' => $username, 'password' => $password]));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
$res = curl_exec($ch);
$data = json_decode($res, true);
$token = $data['data']['token'] ?? null;
echo "Token: $token\n";

if ($token) {
    // Try GetPerguruanTinggi
    $filter = "nama_perguruan_tinggi LIKE '%universitas teknologi%'";
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['act' => 'GetPerguruanTinggi', 'token' => $token, 'filter' => $filter, 'limit' => 5]));
    $res2 = curl_exec($ch);
    echo "GetPerguruanTinggi Result:\n";
    print_r(json_decode($res2, true));
}
