Example PHP

<?php
namespace Telemetric;
class API
{
private $VERSION = 2.1;
private $debug = true;
private $url = '';
private $sessionID = null;
private $errors_url = 'https://telemetric.tech/documentation/';
public $send_headers = [
"Accept" => "Accept: application/json"
];
public $last_headers = [];
public $cookies = [];
public $catchErrors = true;
public function __construct($url)
{
$this->url = $url;
}
public function request($uri, $data = [])
{
$url = $this->url . str_replace('/', '', $uri);
$this->debug('>curl -X POST ' . $url . ' -H "' . implode("; ", array_values($this->send_headers)) . '"', false);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array_values($this->send_headers));
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
if (is_array($data) && count($data)) {
$this->debug(' --data "' . implode("&", array_map(function ($k, $v) {
return $k . '=' . $v;
}, array_keys($data), $data)) . '"');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
} else {
$this->debug();
}
################
try {
$response = curl_exec($ch);
if ($response === false) {
$this->debug(curl_error($ch));
curl_close($ch);
exit(1);
}
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
################
$this->last_headers = [];
$this->cookies = [];
foreach (explode("\r\n", trim($header)) as $row) {
if (preg_match('/(.*?): (.*)/', $row, $matches)) {
$this->last_headers[$matches[1]] = $matches[2];
if ($matches[1] == "Set-Cookie") {
$cookie = trim(explode(';', $matches[2])[0]);
if (trim(explode('=', $cookie)[0]) == "TELEMETRIC") {
$this->cookies[] = $cookie;
$this->sessionID = explode('=', $cookie)[1];
}
}
}
}
if ($this->sessionID) {
$this->send_headers = array_merge($this->send_headers, ["Cookie" => "Cookie: TELEMETRIC=" . $this->sessionID]);
}
if (!$this->isJson($body)) {
$this->debug('Error! Response is not JSON data.');
curl_close($ch);
exit(1);
}
$json = json_decode($body, true);
if ($this->catchErrors) {
$this->onError($json);
}
} catch (\Exception $exception) {
$this->debug($exception->getMessage());
curl_close($ch);
exit(1);
}
curl_close($ch);
return $json;
}
public function debug($text = '', $rn = true, $waitAfter = 0)
{
if (!$this->debug) {
return;
}
#if($rn) echo "\n";
if(is_string($text)) {
echo $text;
} else {
echo json_encode($text, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
}
if($rn) echo "\n";
if ($waitAfter) {
$this->wait($waitAfter);
}
}
public function isJson($string)
{
json_decode($string);
return (json_last_error() == JSON_ERROR_NONE);
}
public function onError($json)
{
if (array_key_exists('error', $json)) {
$this->debug = true;
$this->debug();
$this->debug("Error! code: " . $json['error']['code'] . (array_key_exists('message', $json['error']) ? ', message: ' . $json['error']['message'] : ', see: ' . $this->errors_url));
exit(1);
}
}
public function wait($micro_seconds = 0)
{
usleep($micro_seconds * 1000000);
}
}
$apiUrl = "";
$api = new \Telemetric\API($apiUrl);
$userEmail = "You email";
$userPswd = "You password";
$post = [
"authKey" => md5($userEmail . $userPswd)
];
$api->debug('------------------ TELEMETRIC API ------------------------');
try {
$json_response = $api->request('/api.login', $post);
if ($json_response['login'] === true) {
$api->debug($json_response);
$devices = $api->request('/api.devices.all');
$api->debug($devices);
$api->debug($api->request('/api.logout'));
} else {
$api->debug('Error! Unknown error! Please contact: https://telemetric.tech/documentation/');
}
} catch (Exception $exception) {
$api->debug($exception->getMessage());
}
$api->debug('------------------ TELEMETRIC API ------------------------');

Did this solve your problem?