PHP code example of kaidn / kaidn-php

1. Go to this page and download the library: Download kaidn/kaidn-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/ */

    

kaidn / kaidn-php example snippets


use Kaidn\Client;

$client = new Client();                      // reads KAIDN_API_KEY

$r = $client->score([
    'event' => 'signup',
    'ip'    => $_SERVER['REMOTE_ADDR'],
    'email' => $email,
]);

if ($r->isBlocked()) {
    throw new Denied($r->reason_text);
}

$r = $client->score([
    'event'     => 'signup',
    'user_id'   => $user->id,
    'ip'        => $_SERVER['REMOTE_ADDR'],
    'email'     => $_POST['email'],
    'device_id' => $_POST['kaidn_device_id'] ?? null,   // from @kaidn/fp
]);

$r->verdict;        // "allow" | "review" | "block"
$r->reasons;        // ["datacenter_ip", "disposable_email"]
$r->reason_text;    // a sentence you could send to the customer
$r->score;          // 0-100. Bookkeeping, not a probability

if ($r->isBlocked()) {
    return deny();                      // generic message: a specific one teaches the next attempt
}
if ($r->needsReview()) {
    createAccount($holdRewards = true); // they can use the product, they just cannot earn yet
    flagForReview($r->event_id, $r->reason_text);
} else {
    createAccount();
}

foreach ($r->checks as $c) {
    echo "$c->reason $c->weight $c->key " . json_encode($c->evidence) . "\n";
    // datacenter_ip 45 datacenterIp {"asn":"16509"}
}

use Kaidn\Client;
use Kaidn\CookieOptions;

$client = new Client(null, ['cookie' => new CookieOptions()]);   // off unless you ask

$r = $client->scoreWithCookie(
    ['event' => 'login', 'ip' => $_SERVER['REMOTE_ADDR'], 'email' => $email],
    $_SERVER['HTTP_COOKIE'] ?? null
);

if ($r->set_cookie !== null) {
    header('Set-Cookie: ' . $r->set_cookie, false);
}

if ($r->device && $r->device->resolution === 'deterministic') {
    // we have seen THIS browser, not something that hashes like it
}

if ($r->identity && User::existsByCanonical($r->identity->email_canonical)) {
    return reject('an account already uses this inbox');
}

use Kaidn\KaidnException;

try {
    $r = $client->score(['event' => 'signup', 'email' => $email]);
} catch (KaidnException $e) {
    if ($e->getStatus() === 429) {
        notifyOps('Kaidn quota exhausted');
    }
    throw $e;
}

try {
    $r = $client->score(['event' => 'signup', 'email' => $email]);
} catch (KaidnException $e) {
    $r = null;   // create the account. Do not let our outage become yours.
}

$r->get('a_field_added_after_this_release');
$r->device->get('some_new_signal');
$r->extra;                                    // everything unrecognised

new Client($apiKey, [
    'base_url' => 'https://api.kaidn.io',
    'timeout'  => 10,                      // seconds per attempt
    'retries'  => 2,                       // extra attempts on a transient failure
    'cookie'   => new CookieOptions(),     // omit to keep cookie handling off
]);
bash
composer