Skip to content
InfluxDB (v2+) Client Library for PHP
PHP Shell Makefile Dockerfile
Branch: master
Clone or download

Latest commit

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci ci: removed useless deploy-preview stage Mar 3, 2020
.github Update README with links to CircleCI, CodeCov, Packagist Feb 21, 2020
scripts feat: Checks the health of a running InfluxDB instance by querying th… Mar 30, 2020
spec/InfluxDB2 - Added basic write write support #5 Feb 24, 2020
src/InfluxDB2 feat: Checks the health of a running InfluxDB instance by querying th… Mar 30, 2020
tests feat: Checks the health of a running InfluxDB instance by querying th… Mar 30, 2020
.codecov.yml feat: add User-Agent request header, use Makefile Targets as a base f… Mar 3, 2020
.devcontainer.json Add Point Spec and Implementation with Line Protocol Encoding (Basic) May 14, 2019
.editorconfig Initial Commit Feb 5, 2019
.gitignore feat: add User-Agent request header, use Makefile Targets as a base f… Mar 3, 2020
CHANGELOG.md feat: Checks the health of a running InfluxDB instance by querying th… Mar 30, 2020
Dockerfile Add Point Spec and Implementation with Line Protocol Encoding (Basic) May 14, 2019
LICENSE Update README with links to CircleCI, CodeCov, Packagist Feb 21, 2020
Makefile feat: Checks the health of a running InfluxDB instance by querying th… Mar 30, 2020
README.md feat: Checks the health of a running InfluxDB instance by querying th… Mar 30, 2020
composer.json feat: add User-Agent request header, use Makefile Targets as a base f… Mar 3, 2020
docker-compose.vscode.yml Add Point Spec and Implementation with Line Protocol Encoding (Basic) May 14, 2019
docker-compose.yml feat: add User-Agent request header, use Makefile Targets as a base f… Mar 3, 2020
phpunit.xml Update README with links to CircleCI, CodeCov, Packagist Feb 21, 2020

README.md

influxdb-client-php

CircleCI codecov Packagist Version License GitHub issues GitHub pull requests PHP from Packagist Slack Status

This repository contains the reference PHP client for the InfluxDB 2.0.

Note: This library is for use with InfluxDB 2.x. For connecting to InfluxDB 1.x instances, please use the influxdb-php client.

Disclaimer: This library is a work in progress and should not be considered production ready yet.

Installation

The InfluxDB 2 client is bundled and hosted on https://packagist.org/.

Install the library

The client can be installed with composer.

composer require influxdata/influxdb-client-php

Usage

Creating a client

Use InfluxDB2\Client to create a client connected to a running InfluxDB 2 instance.

$client = new InfluxDB2\Client([
    "url" => "http://localhost:9999",
    "token" => "my-token",
    "bucket" => "my-bucket",
    "org" => "my-org",
    "precision" => InfluxDB2\Model\WritePrecision::NS
]);

Client Options

Option Description Type Default
bucket Default destination bucket for writes String none
org Default organization bucket for writes String none
precision Default precision for the unix timestamps within the body line-protocol String none

Queries

The result retrieved by QueryApi could be formatted as a:

  1. Raw query response
  2. Flux data structure: FluxTable, FluxColumn and FluxRecord
  3. Stream of FluxRecord

Query raw

Synchronously executes the Flux query and return result as unprocessed String

$this->client = new Client([
    "url" => "http://localhost:9999",
    "token" => "my-token",
    "bucket" => "my-bucket",
    "precision" => WritePrecision::NS,
    "org" => "my-org",
    "debug" => false
]);

$this->queryApi = $this->client->createQueryApi();

$result = $this->queryApi->queryRaw(
            'from(bucket:"my-bucket") |> range(start: 1970-01-01T00:00:00.000000001Z) |> last()');

Synchronous query

Synchronously executes the Flux query and return result as a Array of FluxTables

$this->client = new Client([
    "url" => "http://localhost:9999",
    "token" => "my-token",
    "bucket" => "my-bucket",
    "precision" => WritePrecision::NS,
    "org" => "my-org",
    "debug" => false
]);

$this->queryApi = $this->client->createQueryApi();

$result = $this->queryApi->query(
            'from(bucket:"my-bucket") |> range(start: 1970-01-01T00:00:00.000000001Z) |> last()');

Query stream

Synchronously executes the Flux query and return stream of FluxRecord

$this->client = new Client([
    "url" => "http://localhost:9999",
    "token" => "my-token",
    "bucket" => "my-bucket",
    "precision" => WritePrecision::NS,
    "org" => "my-org",
    "debug" => false
]);

$this->queryApi = $this->client->createQueryApi();

$parser = $this->queryApi->queryStream(
            'from(bucket:"my-bucket") |> range(start: 1970-01-01T00:00:00.000000001Z) |> last()');

foreach ($parser->each() as $record)
{
    ...
}

Writing data

The WriteApi supports synchronous and batching writes into InfluxDB 2.0. In default api uses synchronous write. To enable batching you can use WriteOption.

$client = new InfluxDB2\Client(["url" => "http://localhost:9999", "token" => "my-token",
    "bucket" => "my-bucket",
    "org" => "my-org",
    "precision" => InfluxDB2\Model\WritePrecision::NS
]);
$write_api = $client->createWriteApi();
$write_api->write('h2o,location=west value=33i 15');

Time precision

Configure default time precision:

$client = new InfluxDB2\Client(["url" => "http://localhost:9999", "token" => "my-token",
    "bucket" => "my-bucket",
    "org" => "my-org",
    "precision" => \InfluxDB2\Model\WritePrecision::NS
]);

Configure precision per write:

$client = new InfluxDB2\Client([
    "url" => "http://localhost:9999",
    "token" => "my-token",
    "bucket" => "my-bucket",
    "org" => "my-org",
]);

$writeApi = $client->createWriteApi();
$writeApi->write('h2o,location=west value=33i 15', \InfluxDB2\Model\WritePrecision::MS);

Allowed values for precision are:

  • WritePrecision::NS for nanosecond
  • WritePrecision::US for microsecond
  • WritePrecision::MS for millisecond
  • WritePrecision::S for second

Configure destination

Default bucket and organization destination are configured via InfluxDB2\Client:

$client = new InfluxDB2\Client([
    "url" => "http://localhost:9999",
    "token" => "my-token",
    "bucket" => "my-bucket",
]);

but there is also possibility to override configuration per write:

$client = new InfluxDB2\Client(["url" => "http://localhost:9999", "token" => "my-token"]);

$writeApi = $client->createWriteApi();
$writeApi->write('h2o,location=west value=33i 15', \InfluxDB2\Model\WritePrecision::MS, "production-bucket", "customer-1");

Data format

The data could be written as:

  1. string that is formatted as a InfluxDB's line protocol
  2. array with keys: name, tags, fields and time
  3. Data Point structure
  4. Array of above items
$client = new InfluxDB2\Client([
    "url" => "http://localhost:9999",
    "token" => "my-token",
    "bucket" => "my-bucket",
    "org" => "my-org",
    "precision" => InfluxDB2\Model\WritePrecision::US
]);

$writeApi = $client->createWriteApi();

//data in Point structure
$point=InfluxDB2\Point::measurement("h2o")
    ->addTag("location", "europe")
    ->addField("level",2)
    ->time(microtime());

$writeApi->write($point);

//data in array structure
$dataArray = ['name' => 'cpu', 
    'tags' => ['host' => 'server_nl', 'region' => 'us'],
    'fields' => ['internal' => 5, 'external' => 6],
    'time' => microtime()];

$writeApi->write($dataArray);

//write lineprotocol
$writeApi->write('h2o,location=west value=33i 15');

Advanced Usage

Check the server status

Server availability can be checked using the $client->health(); method. That is equivalent of the influx ping.

Local tests

# run unit & integration tests
make test

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/influxdata/influxdb-client-php.

License

The gem is available as open source under the terms of the MIT License.

You can’t perform that action at this time.