PHP#
Library#
There is a convenient PHP library for interacting with Kotori over HTTP ready for download, see
terkin-http.php
.
If you’re stuck with PHP4, see
terkin-http.php4
for a version based on the PHP CURL binding and without namespaces.
Node API (highlevel)#
Transmitting telemetry data using PHP is pretty easy, read on my dear:
1 // Put this file into the folder of your PHP program
2 include("terkin-http.php");
3
4 // Acquire HTTP API library
5 use Terkin\TelemetryNode;
6
7 // Create a "Node API" telemetry client object
8 $telemetry = new TelemetryNode(
9 "http://kotori.example.org/api",
10 array(
11 "realm" => "mqttkit-1",
12 "network" => "testdrive",
13 "gateway" => "area-42",
14 "node" => "node-1",
15 )
16 );
17
18 // Transmit data
19 $data = array("temperature" => 42.84, "humidity" => 83);
20 $telemetry->transmit($data);
Client API (lowlevel)#
For transmitting telemetry data to an absolute uri, use the “Basic API” telemetry client object:
1 // Create a "Basic API" telemetry client object
2 use Terkin\TelemetryClient;
3 $telemetry = new TelemetryClient("http://kotori.example.org/api/mqttkit-1/testdrive/area-42/node-1/data");
Demo#
Command line#
There is a command line program terkin-demo.php
for demonstration purposes, it will send data to localhost:24642
:
php -f clients/runtime/php/terkin-demo.php run demo
php -f clients/runtime/php/terkin-demo.php run sawtooth
Note
24642
is the default http port of Kotori. For making this work, Kotori
should be configured similar to the canonical example configuration described
in MQTTKit application and HTTP acquisition API configuration.
The demo program in detail#
1<?php
2// -*- coding: utf-8 -*-
3/*
4=====================================
5Kotori telemetry demo program for PHP
6=====================================
7
8Documentation
9-------------
10https://getkotori.org/docs/handbook/acquisition/runtime/php.html#demo
11
12
13Synopsis
14--------
15Run demonstration program from the command line::
16
17 # Send fixed measurements "temperature" => 42.84, "humidity" => 83 for demonstration purposes
18 php -f clients/runtime/php/terkin-demo.php run demo
19
20 # Send a periodic, slowly oscillating sawtooth signal
21 php -f clients/runtime/php/terkin-demo.php run sawtooth
22
23*/
24
25namespace DemoProgram {
26
27 // Put this file into the folder of your PHP program
28 include("terkin-http.php");
29
30 // Acquire HTTP API library
31 use Terkin\TelemetryNode;
32
33 // When running from the command line, use some example programs
34 // for submitting telemetry data for demonstration purposes.
35 if (php_sapi_name() == "cli") {
36
37 $telemetry = new TelemetryNode(
38 "http://localhost:24642/api",
39 array(
40 "realm" => "mqttkit-1",
41 "network" => "testdrive",
42 "gateway" => "area-42",
43 "node" => "node-1",
44 )
45 );
46
47 if ($argc > 2) {
48 $command = $argv[1];
49 $subcommand = $argv[2];
50 }
51
52 if ($command == "run") {
53
54 if ($subcommand == "demo") {
55 // Emit single sample of a sawtooth signal
56 $data = array("temperature" => 42.84, "humidity" => 83);
57 var_dump($telemetry->transmit($data));
58
59 } else if ($subcommand == "sawtooth") {
60 date_default_timezone_set("Europe/Berlin");
61 // Emit sample of a sawtooth signal each second, periodically
62 while (true) {
63 $data = array("second" => intval(strftime("%S")));
64 var_dump($telemetry->transmit($data));
65 sleep(1);
66 }
67 }
68 }
69
70 }
71
72}
73
74?>