baseUrl = rtrim($baseUrl ?? (string) env('DAPODIK_BASE_URL', ''), '/'); $this->token = $token ?? (string) env('DAPODIK_TOKEN', ''); $this->npsn = $npsn ?? (string) env('DAPODIK_NPSN', ''); } /** * GET getSekolah * * @return array{success: bool, data?: array, error?: string} */ public function getSekolah(): array { $url = $this->baseUrl . '/getSekolah'; if ($this->npsn !== '') { $url .= '?npsn=' . rawurlencode($this->npsn); } return $this->request('GET', $url); } /** * GET getPesertaDidik with pagination * * @param int $start * @param int $limit * @return array{success: bool, data?: array, rows?: array, id?: mixed, start?: int, limit?: int, results?: int, error?: string} */ public function getPesertaDidik(int $start = 0, int $limit = 200): array { $params = []; if ($this->npsn !== '') { $params['npsn'] = $this->npsn; } $params['start'] = $start; $params['limit'] = $limit; $url = $this->baseUrl . '/getPesertaDidik?' . http_build_query($params); return $this->request('GET', $url); } /** * Execute HTTP request. Returns decoded JSON with success flag and error message on failure. */ protected function request(string $method, string $url): array { $ch = curl_init(); if ($ch === false) { return ['success' => false, 'error' => 'cURL init failed']; } $headers = [ 'Accept: application/json', 'Authorization: Bearer ' . $this->token, ]; curl_setopt_array($ch, [ CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 60, CURLOPT_HTTPHEADER => $headers, CURLOPT_CUSTOMREQUEST => $method, ]); $body = curl_exec($ch); $errno = curl_errno($ch); $httpCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($errno) { return ['success' => false, 'error' => 'Network error: ' . ($errno === CURLE_OPERATION_TIMEDOUT ? 'timeout' : 'curl ' . $errno)]; } $decoded = json_decode($body, true); if (json_last_error() !== JSON_ERROR_NONE) { return ['success' => false, 'error' => 'Invalid JSON response', 'http_code' => $httpCode]; } if ($httpCode < 200 || $httpCode >= 300) { $msg = is_array($decoded) && isset($decoded['message']) ? $decoded['message'] : 'HTTP ' . $httpCode; return ['success' => false, 'error' => $msg, 'http_code' => $httpCode, 'data' => $decoded]; } return array_merge(['success' => true], $decoded); } /** * Normalize Dapodik response to a list of rows. * Dapodik returns { results, id, start, limit, rows: [...] } * * @param array $response Response from getPesertaDidik * @return array> */ public static function normalizePesertaDidikRows(array $response): array { if (! isset($response['rows']) || ! is_array($response['rows'])) { return []; } $rows = $response['rows']; $out = []; foreach ($rows as $i => $row) { $out[] = is_array($row) ? $row : (array) $row; } return $out; } }