influxdb-client-php
This repository contains the reference PHP client for the InfluxDB 2.x.
Note: Use this client library with InfluxDB 2.x and InfluxDB 1.8+ (). For connecting to InfluxDB 1.7 or earlier instances, use the influxdb-php client library.
This section contains links to the client library documentation.
Installation
The InfluxDB 2 client is bundled and hosted on https://packagist.org/.
The client can be installed with composer.
Creating a client
Use to create a client connected to a running InfluxDB 2 instance.
$client = new InfluxDB2\Client([
"url" => "http://localhost:8086",
"token" => "my-token",
"bucket" => "my-bucket",
"org" => "my-org",
"precision" => InfluxDB2\Model\WritePrecision::NS
]);
Client Options
Queries
The result retrieved by could be formatted as a:
- Raw query response
- Flux data structure: FluxTable, and FluxRecord
- Stream of
Query raw
Synchronously executes the Flux query and return result as unprocessed String
$this->client = new Client([
"url" => "http://localhost:8086",
"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:8086",
"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()');
This can then easily be encoded to JSON with
header('Content-type:application/json;charset=utf-8');
echo json_encode( $result, JSON_PRETTY_PRINT ) ;
Query stream
Synchronously executes the Flux query and return stream of FluxRecord
$this->client = new Client([
"url" => "http://localhost:8086",
"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)
{
...
}
InfluxDB Cloud supports that let you dynamically change values in a query using the InfluxDB API. Parameterized queries make Flux queries more reusable and can also be used to help prevent injection attacks.
InfluxDB Cloud inserts the params object into the Flux query as a Flux record named params
. Use dot or bracket notation to access parameters in the params
record in your Flux query. Parameterized Flux queries support only int
, float
, and string
data types. To convert the supported data types into other Flux basic data types, use Flux type conversion functions.
Parameterized query example:
<?php
require __DIR__ . '/../vendor/autoload.php';
use InfluxDB2\Client;
use InfluxDB2\Model\Query;
use InfluxDB2\Point;
use InfluxDB2\WriteType as WriteType;
$url = "https://us-west-2-1.aws.cloud2.influxdata.com";
$organization = 'my-org';
$bucket = 'my-bucket';
$token = 'my-token';
"url" => $url,
"token" => $token,
"bucket" => $bucket,
"org" => $organization,
"precision" => InfluxDB2\Model\WritePrecision::NS,
"debug" => false
]);
$writeApi = $client->createWriteApi(["writeType" => WriteType::SYNCHRONOUS]);
$queryApi = $client->createQueryApi();
$today = new DateTime("now");
$yesterday = $today->sub(new DateInterval("P1D"));
$p = new Point("temperature");
$p->addTag("location", "north")->addField("value", 60)->time($yesterday);
$writeApi->write($p);
$writeApi->close();
//
// Query range start parameter using duration
//
$parameterizedQuery = "from(bucket: params.bucketParam) |> range(start: duration(v: params.startParam))";
$query = new Query();
$query->setQuery($parameterizedQuery);
$query->setParams(["bucketParam" => "my-bucket", "startParam" => "-1d"]);
$tables = $queryApi->query($query);
foreach ($tables as $table) {
foreach ($table->records as $record) {
var_export($record->values);
}
}
//
// Query range start parameter using DateTime
//
$query->setParams(["bucketParam" => "my-bucket", "startParam" => $yesterday]);
$query->setQuery($parameterizedQuery);
$tables = $queryApi->query($query);
foreach ($tables as $table) {
foreach ($table->records as $record) {
var_export($record->values);
}
}
$client->close();
Writing data
The supports synchronous and batching writes into InfluxDB 2.x. In default api uses synchronous write. To enable batching you can use WriteOption.
Batching
The writes are processed in batches which are configurable by WriteOptions
:
use InfluxDB2\Client;
use InfluxDB2\WriteType as WriteType;
$client = new Client(["url" => "http://localhost:8086", "token" => "my-token",
"bucket" => "my-bucket",
"org" => "my-org",
"precision" => InfluxDB2\Model\WritePrecision::NS
]);
$writeApi = $client->createWriteApi(
["writeType" => WriteType::BATCHING, 'batchSize' => 1000]);
foreach (range(1, 10000) as $number) {
$writeApi->write("mem,host=aws_europe,type=batch value=1i $number");
}
// flush remaining data
$writeApi->close();
Time precision
Configure default time precision:
$client = new InfluxDB2\Client(["url" => "http://localhost:8086", "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:8086",
"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 nanosecondWritePrecision::US
for microsecondWritePrecision::MS
for millisecondWritePrecision::S
for second
Configure destination
Default bucket
and organization
destination are configured via InfluxDB2\Client
:
$client = new InfluxDB2\Client([
"url" => "http://localhost:8086",
"token" => "my-token",
"bucket" => "my-bucket",
]);
but there is also possibility to override configuration per write:
$client = new InfluxDB2\Client(["url" => "http://localhost:8086", "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:
string
that is formatted as a InfluxDB’s line protocolarray
with keys: name, tags, fields and time- Data Point structure
Array
of above items
$client = new InfluxDB2\Client([
"url" => "http://localhost:8086",
"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(true));
$writeApi->write($point);
//data in array structure
$dataArray = ['name' => 'cpu',
'tags' => ['host' => 'server_nl', 'region' => 'us'],
'fields' => ['internal' => 5, 'external' => 6],
'time' => microtime(true)];
$writeApi->write($dataArray);
//write lineprotocol
$writeApi->write('h2o,location=west value=33i 15');
Default Tags
Sometimes is useful to store same information in every measurement e.g. hostname
, location
, customer
. The client is able to use static value, app settings or env variable as a tag value.
The expressions:
California Miner
- static value${env.hostname}
- environment property
Via API
Advanced Usage
Check the server status
Server availability can be checked using the $client->ping();
method. That is equivalent of the .
InfluxDB 1.8.0 introduced forward compatibility APIs for InfluxDB 2.x. This allow you to easily move from InfluxDB 1.x to InfluxDB 2.x Cloud or open source.
The following forward compatible APIs are available:
InfluxDB 2.x management API
InfluxDB 2.x API client is generated using . Sources are in InfluxDB2\Service\
and InfluxDB2\Model\
packages.
The following example shows how to use OrganizationService
and BucketService
to create a new bucket.
require __DIR__ . '/../vendor/autoload.php';
use InfluxDB2\Client;
use InfluxDB2\Model\BucketRetentionRules;
use InfluxDB2\Model\PostBucketRequest;
use InfluxDB2\Service\BucketsService;
use InfluxDB2\Service\OrganizationsService;
$organization = 'my-org';
$bucket = 'my-bucket';
$token = 'my-token';
$client = new Client([
"url" => "http://localhost:8086",
"token" => $token,
"bucket" => $bucket,
"org" => $organization,
"precision" => InfluxDB2\Model\WritePrecision::S
]);
function findMyOrg($client): ?Organization
{
/** @var OrganizationsService $orgService */
$orgService = $client->createService(OrganizationsService::class);
$orgs = $orgService->getOrgs()->getOrgs();
foreach ($orgs as $org) {
if ($org->getName() == $client->options["org"]) {
return $org;
}
}
return null;
}
$bucketsService = $client->createService(BucketsService::class);
$rule = new BucketRetentionRules();
$rule->setEverySeconds(3600);
$bucketName = "example-bucket-" . microtime();
$bucketRequest = new PostBucketRequest();
$bucketRequest->setName($bucketName)
->setRetentionRules([$rule])
->setOrgId(findMyOrg($client)->getId());
//create bucket
$respBucket = $bucketsService->postBuckets($bucketRequest);
print $respBucket;
$client->close();
Writing via UDP
Sending via UDP will be useful in cases when the execution time is critical to avoid potential delays (even timeouts) in sending metrics to the InfluxDB while are problems with the database or network connectivity.
As is known, sending via UDP occurs without waiting for a response, unlike TCP (HTTP).
UDP Writer Requirements:
- Installed ext-sockets
- Since Influxdb 2.0+ does not support UDP protocol natively you need to install and configure Telegraf plugin: https://docs.influxdata.com/telegraf/v1.16/plugins/#socket_listener
- Extra config option passed to client: udpPort. Optionally you can specify udpHost, otherwise udpHost will parsed from url option
- Extra config option passed to client: ipVersion. Optionally you can specify the ip version, defaults to IPv4
$client = new InfluxDB2\Client(["url" => "http://localhost:8086", "token" => "my-token",
"bucket" => "my-bucket",
"org" => "my-org",
"precision" => InfluxDB2\Model\WritePrecision::NS,
"udpPort" => 8094,
"ipVersion" => 6,
]);
$writer = $client->createUdpWriter();
$writer->write('h2o,location=west value=33i 15');
$writer->close();
The supports deletes points from an InfluxDB bucket.
<?php
/**
* Shows how to delete data from InfluxDB by client
*/
use InfluxDB2\Client;
use InfluxDB2\Model\DeletePredicateRequest;
use InfluxDB2\Service\DeleteService;
$url = 'http://localhost:8086';
$token = 'my-token';
$org = 'my-org';
$bucket = 'my-bucket';
$client = new Client([
"url" => $url,
"token" => $token,
"bucket" => $bucket,
"org" => $org,
"precision" => InfluxDB2\Model\WritePrecision::S
]);
//
// Delete data by measurement and tag value
//
/** @var DeleteService $service */
$service = $client->createService(DeleteService::class);
$predicate = new DeletePredicateRequest();
$predicate->setStart(DateTime::createFromFormat('Y', '2020'));
$predicate->setStop(new DateTime());
$predicate->setPredicate("_measurement=\"mem\" AND host=\"host1\"");
$service->postDelete($predicate, null, $org, $bucket);
$client->close();
For more details see .
Proxy and redirects
You can configure InfluxDB PHP client behind a proxy in two ways:
1. Using environment variable
Set environment variable HTTP_PROXY
or HTTPS_PROXY
based on the scheme of your server url. For more info see Guzzle docs - environment Variables.
2. Configure client to use proxy via Options
You can pass a proxy
configuration when creating the client:
$client = new InfluxDB2\Client([
"url" => "http://localhost:8086",
"token" => "my-token",
"bucket" => "my-bucket",
"org" => "my-org",
"proxy" => "http://192.168.16.1:10",
]);
For more info see Guzzle docs - .
Redirects
Client automatically follows HTTP redirects. You can configure redirects behaviour by a allow_redirects
configuration:
$client = new InfluxDB2\Client([
"url" => "http://localhost:8086",
"token" => "my-token",
"bucket" => "my-bucket",
"org" => "my-org",
"allow_redirects" => false,
]);
For more info see Guzzle docs - allow_redirects
# run unit & integration tests
make test
Contributing
Bug reports and pull requests are welcome on GitHub at .