PHP code example of confirm-it-solutions / php-zabbix-api

1. Go to this page and download the library: Download confirm-it-solutions/php-zabbix-api 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/ */

    

confirm-it-solutions / php-zabbix-api example snippets



// Load ZabbixApi.

ception;
use Confirm\ZabbixApi\ZabbixApi;

try {
    // Connect to Zabbix API.
    $api = new ZabbixApi(
        'https://zabbix.confirm.ch/api_jsonrpc.php',
        'zabbix_user',
        'zabbix_password'
    );

    // Do your stuff here.
} catch (Exception $e) {
    // Caught exception from ZabbixApi.
    echo $e->getMessage();
}

// Connect to Zabbix API through HTTP basic auth.
$api = new ZabbixApi(
    'https://zabbix.confirm.ch/api_jsonrpc.php',
    'zabbix_user',
    'zabbix_password',
    'http_user',
    'http_password'
);


// This token was previously obtained from a call to the `user.login` method.
$token = 'my_secret_token';

$api = new ZabbixApi(
    'https://zabbix.confirm.ch/api_jsonrpc.php',
    null,
    null,
    null,
    null,
    $token
);

// Make any secured method call.
$api->userGet();

// Using your own HTTP client.

use GuzzleHttp\Client;

$httpClient = new Client([/* Your own config */]);

$api = new ZabbixApi(
    'https://zabbix.confirm.ch/api_jsonrpc.php',
    'zabbix_user',
    'zabbix_password',
    'http_user',
    'http_password',
    null,
    $httpClient
);

// Using custom options fot the built-in HTTP client.

use GuzzleHttp\Client;

$httpClientOptions = [/* Your own config */];

$api = new ZabbixApi(
    'https://zabbix.confirm.ch/api_jsonrpc.php',
    'zabbix_user',
    'zabbix_password',
    'http_user',
    'http_password',
    null,
    null,
    $httpClientOptions
);

/** @var \Psr\Cache\CacheItemPoolInterface $psr6Cache */
$psr6Cache = new Psr6FilesystemAdapter();
$api->setTokenCache($psr6Cache);

// Get all graphs.

/** @var array<array<string, mixed>> $graphs */
$graphs = $api->graphGet();

// Print all graph IDs.
foreach ($graphs as $graph) {
    echo $graph['graphid']."\n";
}

// Get all graphs as instances of `\stdClass`.

/** @var \stcClass[] $graphs */
$graphs = $api->graphGet([], null, false);

// Print all graph IDs.
foreach ($graphs as $graph) {
    echo $graph->graphid."\n";
}

// Get all graphs named "CPU".
$cpuGraphs = $api->graphGet([
    'output' => 'extend',
    'search' => ['name' => 'CPU'],
]);

// Print graph ID with graph name.
foreach ($cpuGraphs as $graph) {
    printf("id:%d name:%s\n", $graph['graphid'], $graph['name']);
}

// Use extended output for all further requests.
$api->setDefaultParams([
    'output' => 'extend',
]);

// Get all graphs named "CPU".
$cpuGraphs = $api->graphGet([
    'search' => ['name' => 'CPU'],
]);

// Print graph ID with graph name.
foreach ($cpuGraphs as $graph) {
    printf("id:%d name:%s\n", $graph['graphid'], $graph['name']);
}

// Get all graphs in an associative array (key=name).
$graphs = $api->graphGet([], 'name');

// Print graph ID with graph name.
if (array_key_exists('CPU Load Zabbix Server', $graphs)) {
    echo 'Graph "CPU Load Zabbix Server" exists.';
} else {
    echo 'Could not find graph "CPU Load Zabbix Server".';
}