<?php

$db = mysqli_connect('127.0.0.1', 'root', '', 'siakad');
$res = mysqli_query($db, "SELECT pengaturan_value FROM pengaturan WHERE pengaturan_key = 'pddikti_url'");
$url = mysqli_fetch_assoc($res)['pengaturan_value'];

$res = mysqli_query($db, "SELECT pengaturan_value FROM pengaturan WHERE pengaturan_key = 'pddikti_username'");
$username = mysqli_fetch_assoc($res)['pengaturan_value'];

$res = mysqli_query($db, "SELECT pengaturan_value FROM pengaturan WHERE pengaturan_key = 'pddikti_password'");
$password = mysqli_fetch_assoc($res)['pengaturan_value'];

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
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, ['Content-Type: application/json']);
$res = json_decode(curl_exec($ch), true);
$token = $res['data']['token'] ?? '';
if (!$token) die("No token\n");

echo "Token: $token\n";

$start = microtime(true);
$payloadPT = json_encode(['act' => 'GetPerguruanTinggi', 'token' => $token, 'filter' => '', 'limit' => 50, 'offset' => 0]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payloadPT);
$resPT = curl_exec($ch);
$time = microtime(true) - $start;

echo "Time taken: $time\n";
echo "Response snippet: " . substr($resPT, 0, 200) . "\n";
curl_close($ch);
