PHPackages                             peynman/influxdb-client-php - 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. peynman/influxdb-client-php

ActiveLibrary

peynman/influxdb-client-php
===========================

InfluxDB (v2+) Client Library for PHP

1.7.0(5y ago)014[1 PRs](https://github.com/peynman/influxdb-client-php/pulls)MITPHPPHP &gt;=7.1

Since Mar 6Pushed 1y agoCompare

[ Source](https://github.com/peynman/influxdb-client-php)[ Packagist](https://packagist.org/packages/peynman/influxdb-client-php)[ Docs](https://www.github.com/influxdata/influxdb-client-php)[ RSS](/packages/peynman-influxdb-client-php/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (5)Versions (9)Used By (0)

influxdb-client-php
===================

[](#influxdb-client-php)

[![CircleCI](https://camo.githubusercontent.com/02aff227bf7116388dab8031abdbc9b4d568ea2fcdbf1857aa9342a5b56baaa4/68747470733a2f2f636972636c6563692e636f6d2f67682f696e666c7578646174612f696e666c757864622d636c69656e742d7068702e7376673f7374796c653d737667)](https://circleci.com/gh/influxdata/influxdb-client-php)[![codecov](https://camo.githubusercontent.com/64a13742aa6532411746bcf6ea94e4f335f2e96c4c156ad839ff441a8646f895/68747470733a2f2f636f6465636f762e696f2f67682f696e666c7578646174612f696e666c757864622d636c69656e742d7068702f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/influxdata/influxdb-client-php)[![Packagist Version](https://camo.githubusercontent.com/3cab1549c4643bc48dd9e6f0518bafc5b1b5d08beb9fe5b9da2624ae98ee53a2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f696e666c7578646174612f696e666c757864622d636c69656e742d706870)](https://packagist.org/packages/influxdata/influxdb-client-php)[![License](https://camo.githubusercontent.com/4e38c9e9f012a82d4d2ed0ba5767a393a7854999260091ffab8514c688160255/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f696e666c7578646174612f696e666c757864622d636c69656e742d7068702e737667)](https://github.com/influxdata/influxdb-client-php/blob/master/LICENSE)[![GitHub issues](https://camo.githubusercontent.com/7ecbbeb3aae7c184223e87f1ccea1e853bf217b4dd556613d0b244067b914678/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732d7261772f696e666c7578646174612f696e666c757864622d636c69656e742d7068702e737667)](https://github.com/influxdata/influxdb-client-php/issues)[![GitHub pull requests](https://camo.githubusercontent.com/dcfb6f12a832ff527e6b86585d46169664dd6cfe627acd120272e0cc2b707225/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732d70722d7261772f696e666c7578646174612f696e666c757864622d636c69656e742d7068702e737667)](https://github.com/influxdata/influxdb-client-php/pulls)[![PHP from Packagist](https://camo.githubusercontent.com/e82a5dec213e5726a193d725523fcb42b25ea85fa7574a06709ae1610524e825/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f696e666c7578646174612f696e666c757864622d636c69656e742d706870)](https://camo.githubusercontent.com/e82a5dec213e5726a193d725523fcb42b25ea85fa7574a06709ae1610524e825/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f696e666c7578646174612f696e666c757864622d636c69656e742d706870)[![Slack Status](https://camo.githubusercontent.com/62df9310aa7255972268f978aea33e09d9e9f5bd811d577c0f743cbc46efa934/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f736c61636b2d6a6f696e5f636861742d77686974652e7376673f6c6f676f3d736c61636b267374796c653d736f6369616c)](https://www.influxdata.com/slack)

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+ ([see details](#influxdb-18-api-compatibility)). For connecting to InfluxDB 1.7 or earlier instances, use the [influxdb-php](https://github.com/influxdata/influxdb-php) client library.

[](#note-use-this-client-library-with-influxdb-2x-and-influxdb-18-see-details-for-connecting-to-influxdb-17-or-earlier-instances-use-the-influxdb-php-client-library)

- [Installation](#installation)
    - [Install the library](#install-the-library)
- [Usage](#usage)
    - [Creating a client](#creating-a-client)
    - [Querying data](#queries)
    - [Writing data](#writing-data)
    - [Default Tags](#default-tags)
- [Advanced Usage](#advanced-usage)
    - [Check the server status](#check-the-server-status)
    - [InfluxDB 1.8 API compatibility](#influxdb-18-api-compatibility)
    - [InfluxDB 2.x management API](#influxdb-2x-management-api)
    - [Writing via UDP](#writing-via-udp)
    - [Delete data](#delete-data)
    - [Proxy and redirects](#proxy-and-redirects)
- [Contributing](#contributing)
- [License](#license)

Documentation
-------------

[](#documentation)

This section contains links to the client library documentation.

- [Product documentation](https://docs.influxdata.com/influxdb/latest/tools/client-libraries/), [Getting Started](#usage)
- [Examples](examples)
- [API Reference](https://influxdata.github.io/influxdb-client-php/)
- [Changelog](CHANGELOG.md)

Installation
------------

[](#installation)

The client is not hard coupled to HTTP client library like Guzzle, Buzz or something else. The client uses general abstractions ([PSR-7 - HTTP messages](https://www.php-fig.org/psr/psr-18/), [PSR-17 - HTTP factories](https://www.php-fig.org/psr/psr-18/), [PSR-18 - HTTP client](https://www.php-fig.org/psr/psr-18/)) which give you freedom to use your favorite one.

### Install the library

[](#install-the-library)

The InfluxDB 2 client is bundled and hosted on [https://packagist.org/](https://packagist.org/packages/influxdata/influxdb-client-php) and can be installed with composer:

```
composer require influxdata/influxdb-client-php guzzlehttp/guzzle

```

Usage
-----

[](#usage)

### Creating a client

[](#creating-a-client)

Use `InfluxDB2\Client` 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

[](#client-options)

OptionDescriptionNoteTypeDefault**url**InfluxDB server API url (e.g. )**required**Stringnone**token**Token to use for the authorization**required**StringnonebucketDefault destination bucket for writesStringnoneorgDefault destination organization for writesStringnoneprecisionDefault precision for the unix timestamps within the body line-protocolStringnoneallow\_redirectsEnable HTTP redirectsbooltruedebugEnable verbose logging of http requestsboolfalselogFileDefault output for logsboolphp://outputhttpClientConfigured HTTP client to use for communication with InfluxDB`Psr\Http\Client\ClientInterface`noneverifySSLTurn on/off SSL certificate verification. Set to `false` to disable certificate verification.⚠️ required `Guzzle` HTTP clientbooltruetimeoutDescribing the number of seconds to wait while trying to connect to a server. Use 0 to wait indefinitely.⚠️ required `Guzzle` HTTP clientint10proxyspecify an HTTP proxy, or an array to specify different proxies for different protocols.⚠️ required `Guzzle` HTTP clientstringnone#### Custom HTTP client

[](#custom-http-client)

The following code shows how to use and configure [cURL](https://github.com/php-http/curl-client) HTTP client:

##### Install dependencies via composer

[](#install-dependencies-via-composer)

```
composer require influxdata/influxdb-client-php nyholm/psr7 php-http/curl-client

```

##### Configure cURL client

[](#configure-curl-client)

```
$curlOptions = [
    CURLOPT_CONNECTTIMEOUT => 30, // The number of seconds to wait while trying to connect.
];
$curlClient = new Http\Client\Curl\Client(
    Http\Discovery\Psr17FactoryDiscovery::findRequestFactory(),
    Http\Discovery\Psr17FactoryDiscovery::findStreamFactory(),
    $curlOptions
);
```

##### Initialize InfluxDB client

[](#initialize-influxdb-client)

```
$client = new Client([
    "url" => "http://localhost:8086",
    "token" => "my-token",
    "bucket" => "my-bucket",
    "org" => "my-org",
    "httpClient" => $curlClient
]);
```

### Queries

[](#queries)

The result retrieved by [QueryApi](https://github.com/influxdata/influxdb-client-php/blob/master/src/InfluxDB2/QueryApi.php) could be formatted as a:

1. Raw query response
2. Flux data structure: [FluxTable](https://github.com/influxdata/influxdb-client-php/blob/master/src/InfluxDB2/FluxTable.php), [FluxColumn](https://github.com/influxdata/influxdb-client-php/blob/master/src/InfluxDB2/FluxColumn.php) and [FluxRecord](https://github.com/influxdata/influxdb-client-php/blob/master/src/InfluxDB2/FluxRecord.php)
3. Stream of [FluxRecord](https://github.com/influxdata/influxdb-client-php/blob/master/src/InfluxDB2/FluxRecord.php)

#### Query raw

[](#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

[](#synchronous-query)

Synchronously executes the Flux query and return result as a Array of [FluxTables](https://github.com/influxdata/influxdb-client-php/blob/master/src/InfluxDB2/FluxTable.php)

```
$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 [json\_encode](https://www.php.net/manual/en/function.json-encode.php)

```
header('Content-type:application/json;charset=utf-8');
echo json_encode( $result, JSON_PRETTY_PRINT ) ;
```

#### Query stream

[](#query-stream)

Synchronously executes the Flux query and return stream of [FluxRecord](https://github.com/influxdata/influxdb-client-php/blob/master/src/InfluxDB2/FluxRecord.php)

```
$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)
{
    ...
}
```

### Parameterized queries

[](#parameterized-queries)

InfluxDB Cloud supports [Parameterized Queries](https://docs.influxdata.com/influxdb/cloud/query-data/parameterized-queries/)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](https://docs.influxdata.com/influxdb/cloud/query-data/parameterized-queries/#supported-parameter-data-types).

Parameterized query example:

> ⚠️ Parameterized Queries are supported only in InfluxDB Cloud, currently there is no support in InfluxDB OSS.

```
