PHP code example of smansage / dapodik-sdk

1. Go to this page and download the library: Download smansage/dapodik-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

smansage / dapodik-sdk example snippets


namespace App\Http\Controllers;

use Smansage\Dapodik\Laravel\Facades\Dapodik;

class SiswaController extends Controller
{
    public function index()
    {
        // 1. Ambil Profil Sekolah
        $sekolah = Dapodik::sekolah()->first();

        // 2. Ambil Siswa (mengembalikan Laravel Collection)
        $siswa = Dapodik::getPesertaDidik(['page' => 1, 'limit' => 50]);

        // Manfaatkan kekuatan Laravel Collection:
        $siswaLakiLaki = $siswa->where('jenis_kelamin', 'L');
        $namaSiswa = $siswa->pluck('nama');

        // 3. Ambil Rombel (Kelas) beserta relasi anggota & pembelajaran
        $rombel = Dapodik::rombel('20241'); // Semester 2024/2025 Ganjil

        return response()->json([
            'sekolah' => $sekolah['nama'] ?? null,
            'total_siswa' => $siswa->count(),
            'laki_laki' => $siswaLakiLaki->count(),
            'daftar_nama' => $namaSiswa,
            'rombel' => $rombel,
        ]);
    }
}

use Smansage\Dapodik\DapodikClient;

host' => '192.168.1.100',
    'port' => 5774,
    'npsn' => '20300001',
    'token' => 'TOKEN_WEBSERVICE_DAPODIK',
    'timeout' => 30.0,
]);

// 1. Ambil Profil Sekolah
$sekolah = $client->getSekolah();
echo "Sekolah: " . $sekolah->first()['nama'] . "\n";

// 2. Ambil Siswa
$siswa = $client->pd(['page' => 1, 'limit' => 50]);
echo "Jumlah siswa ditarik: " . $siswa->count() . "\n";

// Tarik seluruh siswa sekolah secara otomatis
$semuaSiswa = $client->fetchAllPesertaDidik(
    limit: 100,
    delayMs: 150, // Jeda 150ms per halaman agar server Dapodik tidak overload
    onProgress: function (int $page, int $batchCount, int $totalCount) {
        echo "Halaman {$page}: ditarik +{$batchCount} siswa (Total terakumulasi: {$totalCount})\n";
    }
);

echo "Total seluruh siswa: " . $semuaSiswa->count() . "\n";

use Smansage\Dapodik\Exceptions\DapodikAuthException;
use Smansage\Dapodik\Exceptions\DapodikConnectionException;
use Smansage\Dapodik\Exceptions\DapodikHttpException;
use Smansage\Dapodik\Exceptions\DapodikException;

try {
    $siswa = Dapodik::getPesertaDidik();
} catch (DapodikAuthException $e) {
    // Error 401/403: Token salah atau IP client belum di-whitelist di Dapodik
    logger()->error("Autentikasi gagal: " . $e->getMessage());
} catch (DapodikConnectionException $e) {
    // Server Dapodik mati / port 5774 tidak bisa diakses
    logger()->error("Koneksi gagal: " . $e->getMessage());
} catch (DapodikHttpException $e) {
    // Error HTTP 500/404 dari WebService
    logger()->error("HTTP Error [{$e->getCode()}]: " . $e->getMessage());
} catch (DapodikException $e) {
    // Error umum lainnya
    logger()->error("Dapodik Error: " . $e->getMessage());
}
bash
php artisan vendor:publish --tag=dapodik-config