<?php
$conn = new mysqli('localhost', 'root', '', 'siakad');
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }
$res = $conn->query("SELECT * FROM pengaturan WHERE pengaturan_key IN ('pddikti_url', 'pddikti_username', 'pddikti_password')");
$settings = [];
while ($row = $res->fetch_assoc()) { $settings[$row['pengaturan_key']] = $row['pengaturan_value']; }

$ch = curl_init($settings['pddikti_url']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['act'=>'GetToken','username'=>$settings['pddikti_username'],'password'=>$settings['pddikti_password']]));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
$res = curl_exec($ch);
$token = json_decode($res, true)['data']['token'];

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
    'act' => 'GetWilayah',
    'token' => $token,
    'filter' => "nama_wilayah like '%praya%'",
    'limit' => 2
]));
$resWil = curl_exec($ch);
curl_close($ch);

file_put_contents('test_wilayah_result.json', $resWil);
