SIMBA API v2.1.0 is now fully compatible with Laravel 8+. This guide shows you how to use it correctly in your Laravel application.
composer require simba/api:^2.1If you want to publish the configuration file:
php artisan vendor:publish --provider="simba\api\Laravel\SimbaServiceProvider"This creates config/simba.php where you can customize settings.
The simplest and most reliable way to use SIMBA API in Laravel is direct instantiation:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use simba\api\Libraries\Muzakki;
use simba\api\Config\Simba as SimbaConfig;
class DonationController extends Controller
{
public function datamuzakki(Request $request)
{
try {
$param = array(
'p' => $request->get('page') ?? 1,
'platform' => $request->get('platform') ?? 'web',
'keyword' => $request->get('keyword') ?? '',
'tipe' => $request->get('tipe') ?? 'perorangan',
'email' => $request->get('email') ?? SimbaConfig::getAdminEmail(),
);
// β
Direct instantiation - Simplest and works perfectly
$result = (new Muzakki())->getList($param);
return response()->json($result);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'message' => $e->getMessage()
], 500);
}
}
}Why this works:
- When you create
new Muzakki(), it calls__construct($httpClient = null) - With
$httpClient = null, the Client automatically detects Laravel Http Facade - It uses Laravel's native HTTP client without any additional setup
- Clean, simple, and reliable β
You can use all 5 SIMBA libraries the same way:
use simba\api\Libraries\Muzakki;
use simba\api\Libraries\Mustahik;
use simba\api\Libraries\Pengumpulan;
use simba\api\Libraries\Penyaluran;
use simba\api\Libraries\Upz;
// Create instances and use
$muzakki = new Muzakki();
$mustahik = new Mustahik();
$pengumpulan = new Pengumpulan();
$penyaluran = new Penyaluran();
$upz = new Upz();
// Then call methods
$muzakki->getList($params);
$mustahik->getList($params);
$pengumpulan->getList($params);
$penyaluran->getList($params);
$upz->getList($params);$muzakki = new Muzakki();
$param = [
'p' => 1,
'platform' => 'web',
'keyword' => '',
'tipe' => 'perorangan',
'email' => SimbaConfig::getAdminEmail(),
];
$result = $muzakki->getList($param);
if ($result['success']) {
return response()->json($result['data']);
} else {
return response()->json(['error' => $result['message']], 400);
}$muzakki = new Muzakki();
$result = $muzakki->search('Ahmad Syaiful', ['q' => 'npwz']);
return response()->json($result);$muzakki = new Muzakki();
$result = $muzakki->getTotalDonasi('npwz_value', '2024');
return response()->json($result);$muzakki = new Muzakki();
$param = [
'dari' => '01/01/2024',
'hingga' => '31/12/2024',
'npwz' => 'npwz_value',
];
$result = $muzakki->getLaporanDonasi($param);
return response()->json($result);If you prefer Laravel's dependency injection pattern:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use simba\api\Libraries\Muzakki;
class DonationController extends Controller
{
protected $muzakki;
// β
Laravel will auto-inject Muzakki instance
public function __construct(Muzakki $muzakki)
{
$this->muzakki = $muzakki;
}
public function datamuzakki(Request $request)
{
try {
$param = [
'p' => $request->get('page') ?? 1,
'platform' => $request->get('platform') ?? 'web',
'keyword' => $request->get('keyword') ?? '',
'tipe' => $request->get('tipe') ?? 'perorangan',
'email' => $request->get('email'),
];
// β
Use injected instance
$result = $this->muzakki->getList($param);
return response()->json($result);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'message' => $e->getMessage()
], 500);
}
}
}Important: For this to work, you need to register the ServiceProvider in config/app.php:
'providers' => [
// ... other providers ...
\simba\api\Laravel\SimbaServiceProvider::class,
],Create or update your .env file:
SIMBA_BASE_URL=https://api.baznas.go.id
SIMBA_ORG_CODE=your_org_code
SIMBA_API_KEY=your_api_key
SIMBA_TIMEOUT=5
SIMBA_ADMIN_EMAIL=admin@example.comphp artisan vendor:publish --provider="simba\api\Laravel\SimbaServiceProvider"This creates config/simba.php:
return [
'base_url' => env('SIMBA_BASE_URL', 'https://api.baznas.go.id'),
'org_code' => env('SIMBA_ORG_CODE'),
'api_key' => env('SIMBA_API_KEY'),
'timeout' => env('SIMBA_TIMEOUT', 5),
'admin_email' => env('SIMBA_ADMIN_EMAIL', 'admin@example.com'),
];Test your integration:
// In your controller or route
Route::get('/test-simba', function () {
try {
$muzakki = new Muzakki();
$result = $muzakki->getList();
return response()->json([
'success' => true,
'message' => 'SIMBA API is working!',
'data' => $result
]);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'message' => $e->getMessage()
], 500);
}
});Visit: http://your-laravel-app.test/test-simba
When you instantiate a library (e.g., new Muzakki()):
-
Check for Laravel Http Facade
- If available β Use it β
- This is automatic in Laravel 8+
-
Check for CodeIgniter Services
- If available β Use it β
- Useful for hybrid applications
-
Check for PHP cURL extension
- If available β Ready for fallback
- Safe to proceed
-
If none available β Throw error with helpful message
Laravel Controller
β
new Muzakki()
β
Client::__construct($httpClient = null)
β
Detects Laravel Http Facade
β
Sets $this->client = null (uses facade in sendRequest)
β
getList($param) calls sendRequest()
β
Uses \Illuminate\Support\Facades\Http::get() or ::post()
β
Returns JSON response
β
Controller returns response()->json($result)
Cause: Laravel Http Facade not detected
Solution:
- Ensure Laravel 8+ is installed
- Check
php artisan --version - Verify
composer.jsonhas Laravel dependencies
# Test Laravel Http availability
php artisan tinker
>>> class_exists('Illuminate\Support\Facades\Http')Cause: Wrong method name or typo
Solution: Use exact method names from library:
getList()- Get listsearch()- SearchgetTotalDonasi()- Get total donationgetLaporanDonasi()- Get report
Cause: Invalid parameters or API error
Solution:
- Check response:
dd($result) - Verify parameters match API documentation
- Check API credentials in
.env
$result = $muzakki->getList($param);
// Always check success flag
if (!$result['success']) {
dd($result['message']); // Debug the error
}| Method | Parameters | Returns |
|---|---|---|
getList($params) |
p, platform, keyword, tipe, email |
Array of muzakki |
search($query, $params) |
query, q |
Search results |
getTotalDonasi($npwz, $tahun) |
npwz, tahun |
Total donation amount |
getLaporanDonasi($params) |
dari, hingga, npwz, jenis |
Donation report |
| Method | Parameters | Returns |
|---|---|---|
getList($params) |
p, platform, keyword, tipe |
Array of mustahik |
search($query) |
query |
Search results |
| Method | Parameters | Returns |
|---|---|---|
getList($params) |
p, platform, keyword |
Array of pengumpulan |
| Method | Parameters | Returns |
|---|---|---|
getList($params) |
p, platform, keyword |
Array of penyaluran |
| Method | Parameters | Returns |
|---|---|---|
getList($params) |
p, platform, keyword |
Array of upz |
β DO:
- Use direct instantiation:
new Muzakki() - Check
$result['success']before accessing data - Use try-catch for error handling
- Cache API responses when possible
- Store credentials in
.env
β DON'T:
- Hardcode API credentials
- Call API on every page load
- Ignore error messages
- Mix framework APIs (CodeIgniter helpers in Laravel)
- Use deprecated method names
Before deploying to production:
- API credentials set in
.env - All required parameters configured
- Error handling implemented
- API responses cached appropriately
- Logging implemented for debugging
- Rate limiting considered
- Timeout values appropriate
If you encounter issues:
- Check error message carefully - It usually tells you what's wrong
- Enable debug mode: Set
APP_DEBUG=truein.env - Check logs:
storage/logs/laravel.log - Verify API credentials: Ensure
SIMBA_*vars are set correctly - Test API directly: Use Postman with your credentials
SIMBA API v2.1.0 is production-ready for Laravel! π
Simplest working code:
$result = (new Muzakki())->getList($param);
return response()->json($result);That's it! The client automatically handles HTTP requests using Laravel's native Http Facade.
Happy coding! π