Skip to content

Configuration

BioTime resolves its configuration lazily — nothing is sent over the network until you call a resource method. There are three ways to configure it, all backed by the same Config class.

Configuration options

These are every option Config accepts (src/Config.php):

OptionConstructor property.env variable (via Config::auto())DefaultDescription
Host / IPhostBIOTIME_IP (falls back to BIOTIME_HOST)127.0.0.1Device or server hostname/IP
PortportBIOTIME_PORT8081Device or server port
UsernameusernameBIOTIME_USERNAME''API username used to obtain a token
PasswordpasswordBIOTIME_PASSWORD''API password used to obtain a token
HTTPShttpsBIOTIME_HTTPSfalseUse https:// instead of http://
Verify TLS certificateverifysee note belowfalseVerify the HTTPS server certificate
TimeouttimeoutBIOTIME_TIMEOUT15Guzzle request timeout, in seconds
Token TTLtokenTtlSecondsBIOTIME_TOKEN_TTL43200 (12h)How long a cached token is considered valid
Token cache keytokenCacheKeyBIOTIME_TOKEN_CACHE_KEYzkteco_device_tokenCache key used to store the token
Cachecache— (object only)null (in-memory)Any PSR-16 Psr\SimpleCache\CacheInterface

WARNING

The TLS-verification environment variable has an inconsistency worth knowing about: the shipped src/config/biotime.php config file reads BIOTIME_VERIFY_HTTPS, but Config::fromEnv() (used when no config file is present) currently reads a variable literally named BIOTIME_verify (lower-case verify, prefixed with BIOTIME_) rather than BIOTIME_VERIFY_HTTPS. If you rely on plain environment variables (no config/biotime.php file) and need to control certificate verification, either set both variable names, or configure verify explicitly via Config::fromArray() to avoid depending on the env-var name.

Option A — environment variables / .env

If no config/biotime.php file exists at your working directory, Config::auto() falls back to reading environment variables (via getenv(), $_ENV, or $_SERVER) with the BIOTIME_ prefix:

dotenv
BIOTIME_IP=192.168.1.50
BIOTIME_PORT=8081
BIOTIME_USERNAME=admin
BIOTIME_PASSWORD=secret
BIOTIME_HTTPS=false
BIOTIME_TIMEOUT=15
BIOTIME_TOKEN_TTL=43200
php
use BioTime\BioTime;

// Loads ./config/biotime.php if it exists, otherwise reads BIOTIME_* env vars
$biotime = new BioTime();

You can also build a Config directly from environment variables with a custom prefix:

php
use BioTime\Config;

$config = Config::fromEnv('BIOTIME_');

Option B — a config/biotime.php file

The package ships a template at src/config/biotime.php. Copy it into your project's config/ directory (relative to your working directory) and edit it directly. It still falls back to $_ENV/getenv() values, so it's safe to commit without hard-coding secrets:

php
<?php

return [
    'ip'       => $_ENV['BIOTIME_IP']       ?? getenv('BIOTIME_IP')       ?: '192.168.1.50',
    'port'     => (int) ($_ENV['BIOTIME_PORT'] ?? getenv('BIOTIME_PORT') ?: 8081),
    'username' => $_ENV['BIOTIME_USERNAME'] ?? getenv('BIOTIME_USERNAME') ?: 'admin',
    'password' => $_ENV['BIOTIME_PASSWORD'] ?? getenv('BIOTIME_PASSWORD') ?: 'secret',
    'https'    => filter_var($_ENV['BIOTIME_HTTPS'] ?? getenv('BIOTIME_HTTPS') ?: false, FILTER_VALIDATE_BOOLEAN),
    'verify'   => filter_var($_ENV['BIOTIME_VERIFY_HTTPS'] ?? getenv('BIOTIME_VERIFY_HTTPS') ?: false, FILTER_VALIDATE_BOOLEAN),
    'timeout'  => (int) ($_ENV['BIOTIME_TIMEOUT'] ?? getenv('BIOTIME_TIMEOUT') ?: 15),
];

Config::auto() looks for this file at getcwd() . '/config/biotime.php' by default, or you can pass an explicit path:

php
use BioTime\Config;

$config = Config::fromFile(__DIR__ . '/config/biotime.php');

Option C — build Config programmatically

Useful for multiple devices, a custom cache, or tests:

php
use BioTime\BioTime;
use BioTime\Config;

$config = Config::fromArray([
    'ip'       => '192.168.1.50',
    'port'     => 8081,
    'username' => 'admin',
    'password' => 'secret',
    'https'    => false,
    'verify'   => true,
    'timeout'  => 15,
    'cache'    => $psr16Cache, // optional, any PSR-16 CacheInterface
]);

$biotime = new BioTime($config);

Config::fromArray() also accepts host as an alternative key to ip, token_ttl, and token_cache_key.

Caching the token

If you pass a PSR-16 cache implementation, the device token is stored there under tokenCacheKey for tokenTtlSeconds seconds. If you don't provide one, the SDK falls back to an in-memory static store that only lives for the current PHP process — fine for a single script or one-off job, but it won't persist the token across separate HTTP requests. See Caching for details, and Laravel for how the Laravel provider wires up the app's cache store automatically.

Next step

Continue to Quick start.

Distributed under the MIT License.