PHP code example of rconfig / laravel-zabbix
1. Go to this page and download the library: Download rconfig/laravel-zabbix 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/ */
rconfig / laravel-zabbix example snippets
return [
'base_url' => env('ZABBIX_BASE_URL', 'http://localhost'),
'endpoint' => env('ZABBIX_ENDPOINT', '/api_jsonrpc.php'),
// Prefer API token (Zabbix 5.4+)
'token' => env('ZABBIX_API_TOKEN'),
// Legacy authentication
'username' => env('ZABBIX_USERNAME'),
'password' => env('ZABBIX_PASSWORD'),
// HTTP settings
'timeout' => (int) env('ZABBIX_TIMEOUT', 15),
'retries' => (int) env('ZABBIX_RETRIES', 2),
'retry_sleep_ms' => (int) env('ZABBIX_RETRY_SLEEP_MS', 250),
// Testing
'fake_by_default' => env('ZABBIX_FAKE_BY_DEFAULT', true),
];
use Rconfig\Zabbix\Exceptions\UnsupportedOperationException;
// Connect with credentials
$zabbix = ZabbixApi::login('https://zabbix.example.com', 'Admin', 'zabbix');
// Or use API token (recommended)
$zabbix = ZabbixApi::login('https://zabbix.example.com', null, null, [
'token' => 'your_api_token_here'
]);
// Get API version
$version = $zabbix->apiVersion(); // "7.0.0"
// List all hosts
$hosts = $zabbix->hosts()->all();
// Find active Linux servers with fluent syntax
$activeLinux = $zabbix->hosts()
->where(['status' => 0, 'name' => 'linux-server'])
->get();
// Create a new host
$newHost = $zabbix->hosts()->create([
'host' => 'app-01',
'interfaces' => [[
'type' => 1, 'main' => 1, 'useip' => 1,
'ip' => '10.0.0.50', 'port' => '10050'
]],
'groups' => [['groupid' => '2']],
]);
$zabbix = ZabbixApi::login($url, null, null, ['token' => $token]);
// Simple fluent API - much cleaner!
$hosts = $zabbix->hosts()
->limit(50)
->select(['hostid', 'host', 'status'])
->withInterfaces()
->withGroups()
->where(['status' => 0])
->sort('host')
->get();
// Get count of hosts
$totalHosts = $zabbix->hosts()->count();
// or
$totalHosts = $zabbix->hosts()->countOnly()->get();
$groups = $zabbix->hostGroups()
->limit(20)
->select(['groupid', 'name'])
->get();
$hosts = $zabbix->hosts()->get(
$zabbix->hosts()->query()
->select(['hostid', 'host', 'status'])
->withInterfaces()
->withGroups()
->where(['status' => 0])
->limit(50)
->sort('host')
);
use Rconfig\Zabbix\Exceptions\ZabbixException;
try {
Zabbix::apiinfo()->delete(['id' => 1]);
} catch (UnsupportedOperationException $e) {
echo $e->getMessage(); // "Delete is not supported for Apiinfo resource"
}
---
## 🛡️ Lightweight Parameter Validation
An **optional** guard can warn or throw if obviously invalid parameters are passed.
This is not a full schema validator — just common-sense checks to help catch mistakes early.
* Wrong data types for known fields
* Mutually exclusive params used together
* Missing
// Will trigger a warning about 'limit' being a string instead of an integer
Zabbix::hosts()->get(['limit' => 'fifty']);
$zabbix = ZabbixApi::login('https://zabbix.example.com', null, null, [
'token' => 'your_api_token_here'
]);
$zabbix = ZabbixApi::login('https://zabbix.example.com', 'Admin', 'zabbix');
$zabbix = ZabbixApi::login('https://zabbix.example.com', 'Admin', 'zabbix', [
// SSL options
'sslVerifyPeer' => false,
'sslVerifyHost' => false,
// Timeout settings
'timeout' => 30,
'connectTimeout' => 10,
// Debug and compression
'debug' => true,
'useGzip' => true
]);
$zabbix = ZabbixApi::login([
'url' => config('zabbix.base_url'),
'token' => config('zabbix.token'),
]);
// All calls return realistic mock data
$zabbix = ZabbixApi::login('fake://localhost');
$hosts = $zabbix->hosts()->all(); // Returns mock data
$zabbix = ZabbixApi::login($url, null, null, ['token' => $token]);
it('fetches hosts via fluent query', function () {
$zabbix = ZabbixApi::login('fake://localhost');
$hosts = $zabbix->hosts()->all();
expect($hosts)->toBeArray()->not->toBeEmpty();
});
use Rconfig\Zabbix\Exceptions\ZabbixException;
use Rconfig\Zabbix\Exceptions\ZabbixHttpException;
use Rconfig\Zabbix\Facades\ZabbixApi;
try {
$zabbix = ZabbixApi::login($url, null, null, ['token' => $token]);
$hosts = $zabbix->hosts()->all();
} catch (ZabbixException $e) {
// API-level errors (invalid params, auth issues, etc.)
logger()->error('Zabbix API error: ' . $e->getMessage());
} catch (ZabbixHttpException $e) {
// HTTP transport errors (timeouts, connectivity, etc.)
logger()->error('Zabbix HTTP error: ' . $e->getMessage());
}
// routes/web.php
Route::get('/zabbix-test', function () {
$zabbix = ZabbixApi::login(['url' => 'fake://localhost']);
return [
'version' => $zabbix->apiVersion(),
'hosts' => $zabbix->hosts()->all(5),
];
});
bash
php artisan vendor:publish --tag=zabbix-config