Skip to main content

Write data to M3DB with PHP

This example writes some data to an M3DB service from PHP, making use of the InfluxDB® library.

Variables

These are the placeholders you will need to replace in the code sample:

VariableDescription
SERVICE_HOSTService hostname, found on the service overview page
SERVICE_PORTService port number, found on the service overview page
AVNADMIN_PASSPassword for the default avnadmin user

Prerequisites

For this example you will need:

  1. The PHP InfluxDB library. You can install this with composer:

    composer require influxdata/influxdb-client-php
note

M3DB supports InfluxDB® (v1) protocol so we can use the existing library for this database too.

Code

Add the following to index.php and replace the placeholders with values for your project:

<?php

require "vendor/autoload.php";

// create a client
$client = new InfluxDB\Client(
SERVICE_HOST,
SERVICE_PORT,
"avnadmin",
AVNADMIN_PASS,
true,
true,
);

// construct a data point
$point = new InfluxDB\Point(
'php_example_metric', // name of the measurement
0.64, // the measurement value
['host' => 'server1', 'location' => 'EU-DE-22'], // optional tags
['cpucount' => 8], // optional additional fields
);

// write to the database using the path
$result = $client->write(["url" => "api/v1/influxdb/write?db=default"], $point);
var_dump($result);

This code creates an InfluxDBClient and connects to the InfluxDB-literate endpoint on the M3DB. Then the code constructs the expected data format, and writes it to the client.

To run the code:

php -f index.php

If the script outputs bool(true) then there is data in your M3DB. See Visualize M3DB data with Grafana.