PHPackages                             kamermans/zabbix-client - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. kamermans/zabbix-client

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

kamermans/zabbix-client
=======================

v1.0.0(9y ago)33405MITPHPPHP &gt;=5.4.0

Since Apr 17Pushed 9y ago2 watchersCompare

[ Source](https://github.com/kamermans/zabbix-client)[ Packagist](https://packagist.org/packages/kamermans/zabbix-client)[ RSS](/packages/kamermans-zabbix-client/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (1)Versions (3)Used By (0)

zabbix-client
=============

[](#zabbix-client)

This is a simple, yet powerful Zabbix API client for PHP. I have made no attempt to model the Zabbix API in PHP functions, but rather, I have made the interface extremely easy to use in order to query the Zabbix JSON-RPC API.

There are several examples in `examples/` that should be quite useful. You can even make a copy of `zabbix_auth.php` and call it `zabbix_auth_local.php` and that file will be used by the examples instead, so you don't need to taint the git repo.

Compatibility
-------------

[](#compatibility)

This library is designed and tested for Zabbix 2.2 and it's likely to have problems with other versions. If you want to add support for 2.4 (or any other version) you are more than welcome :)

Authentication
--------------

[](#authentication)

Zabbix uses a username and password to connect initially, then an auth token for subsequent requests.

Here's how you connect and authenticate:

```
$client = new ZabbixClient(
    "http://myzabbixserver/zabbix/api_jsonrpc.php",
    "myusername",
    "mypassword"
);
```

Calling Zabbix Endpoints
------------------------

[](#calling-zabbix-endpoints)

All the Zabbix endpoints are described in the [Zabbix Documentation](https://www.zabbix.com/documentation/2.2/manual/api).

To call and endpoint, use the `$client->request(string endpoint, array params=[])` method:

```
$response = $client->request('apiinfo.version');
echo "Server is running Zabbix $response\n";
```

Note that `$response` is a `kamermans\ZabbixClient\ApiResponse` object that has some magical properties. In the example above, the Zabbix endpoint `apiinfo.version` returns a single string, so you can use `$response` as a string as well.

Most endpoints return an associative array as a response:

```
$hosts = $client->request('host.get', [
    'output' => [
        'host_id',
        'host',
    ],
]);

foreach ($hosts as $host) {
    echo "{$host['hostid']}: {$host['host']}\n";
}
```

As you can see, you can iterate over the response object (although it's still a `kamermans\ZabbixClient\ApiResponse`).

In this example we also passed an array of parameters as the second argument.

Error Handling
--------------

[](#error-handling)

When the Zabbix API returns an error, it is wrapped up into a proper exception and thrown.

There are three types of exceptions that you can catch inside the `kamermans\ZabbixClient` namespace:

- `Exception`: base class used for generic exceptions. All exceptions extend this one.
- `ApiException`: all API calls that returned a parseable JSON response are wrapped by this exception which gives you a well-formed error message from Zabbix and access to raw response via `ApiException::getResponse()`.
- `AuthException`: an extention of `ApiException` that only gets thrown for authentication errors so they can be handled differently.

Utility Functions
-----------------

[](#utility-functions)

### Response Dumping

[](#response-dumping)

For easy troubleshooting of responses, you can use the `ApiResponse::dump()` function to output the response to the screen, or `ApiResponse::dd()` to dump and die:

```
$response = $client->request('apiinfo.version');
$response->dd(); // '2.2.8'
```

### Server Ping

[](#server-ping)

There is a utility function to check to latency (in milliseconds) to the Zabbix Server API:

```
$time = $client->ping();
echo "Pinged Zabbix Server in {$time}ms\n";
```

Complete Example
----------------

[](#complete-example)

This example lists all the system items in the Zabbix Server:

```
