PHP code example of mirafive / sdk-php

1. Go to this page and download the library: Download mirafive/sdk-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mirafive / sdk-php example snippets


use MiraFive\Mira;

$mira = new Mira(); // reads MIRAFIVE_SECRET_KEY (and MIRAFIVE_HOST, if set)

$mira->track('signup', userId: 'u_42', properties: ['plan' => 'pro']);
$mira->identify('u_42', ['plan' => 'pro', 'company' => 'Example GmbH']);

// Sent when the request ends. To send now:
$mira->flush();

$receipt = $mira->send([
    ['name' => 'order completed', 'userId' => 'u_42', 'properties' => ['revenue' => 129, 'currency' => 'EUR']],
], idempotencyKey: 'order-981');

use MiraFive\Mode;

$mira = new Mira(mode: Mode::Consentless);
$mira->track('invoice paid', properties: ['revenue' => 99, 'currency' => 'EUR']);

new Mira(
    key: null,              // string|false|null. null reads MIRAFIVE_SECRET_KEY; getenv() results are accepted as-is
    host: null,             // null reads MIRAFIVE_HOST, else Mira::DEFAULT_HOST (https://events.mirafive.io). A scheme is neMs: 3_000, // what one flush may spend on attempts and waits together
    maxRetries: 2,          // for 408, 429, 5xx, timeouts and network errors
    maxRetryAfterMs: 3_000, // a Retry-After longer than this ends the retries instead of stalling your request
    transport: null,        // MiraFive\Http\Transport; default CurlTransport, else StreamTransport
    onError: null,          // callable(MiraError): void, receives every delivery failure
    logger: null,           // Psr\Log\LoggerInterface, used when there is no onError
    cache: null,            // Psr\SimpleCache\CacheInterface, shared between processes: the delivery breaker and all flag state
    enabled: true,          // false: nothing leaves the process and no key is needed; input is still checked
    flushOnShutdown: true,  // false: no shutdown function (your framework flushes on terminate)
    flagsRefreshSeconds: 30,// refresh interval of flags()
    handOff: null,          // Closure(string $body, string $batchId): void, receives buffered batches instead of sending them
);

use MiraFive\Http\Psr18Transport;

$mira = new Mira(transport: new Psr18Transport($client, $requestFactory, $streamFactory));

$flags = $mira->flags(); // or new MiraFive\Flags\MiraFlags(key: …, cache: $psr16Cache)

$user = $flags->for(
    userId: 'u_42',
    properties: ['plan' => 'pro'],
    consent: ['experiments' => true, 'targeting' => false], // optional: the banner's answer
    optedOut: false,                                        // true when the request carries Sec-GPC: 1 or DNT: 1
);

$user->enabled('new-checkout');            // bool
$user->variant('pricing-test');            // ?string
$user->config('limits', ['max' => 3]);     // the variant's value, objects as arrays
$user->evaluate('pricing-test');           // Evaluation: variant, reason, rule, errorCode, value

new MiraFlags(
    key: null,              // null reads MIRAFIVE_SECRET_KEY
    host: null,
    refreshSeconds: 30,
    timeoutMs: 1_500,       // the document fetch
    lookupTimeoutMs: 500,   // the segment lookup
    connectTimeoutMs: 1_000,
    cache: null,            // Psr\SimpleCache\CacheInterface
    document: null,         // a snapshot (a snapshot() string, or an array), used while none was fetched and while younger than 7 days
    enabled: true,          // false: never fetches or looks up, so reads answer their fallbacks
    mira: null,             // sends exposures; defaults to a client on the same key
    transport: null,
    onError: null,
    logger: null,
);

foreach (MiraFive\Flags\MiraFlags::BOOTSTRAP_HEADERS as $name => $value) {
    header("{$name}: {$value}"); // Cache-Control: private, no-store
}

echo $user->bootstrap(); // <script type="application/json" id="mirafive-flags">…</script>
bash
composer