PHP code example of tigusigalpa / yandex-smartcaptcha-php

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

    

tigusigalpa / yandex-smartcaptcha-php example snippets


use Tigusigalpa\YandexSmartCaptcha\SmartCaptchaClient;

// Create client with OAuth token
// The package will automatically exchange it for IAM token and refresh when needed
$client = new SmartCaptchaClient($oauthToken);

// Validate user token
$result = $client->validate(
    token: $_POST['smart-token'],
    secret: 'your-server-secret-key',
    ip: $_SERVER['REMOTE_ADDR']
);

if ($result->isValid()) {
    echo "✅ Human verified!";
} else {
    echo "❌ Bot detected!";
}

use Tigusigalpa\YandexSmartCaptcha\Laravel\Facades\SmartCaptcha;

// Validate token
$result = SmartCaptcha::validate(
    request()->input('smart-token'),
    config('smartcaptcha.secret_key'),
    request()->ip()
);

if ($result->isValid()) {
    // User is verified
}

$result = $client->validate(
    token: 'user-token',
    secret: 'server-secret-key',
    ip: '192.168.1.1' // optional but recommended
);

// Check result
if ($result->isValid()) {
    echo "Status: {$result->status}";
    echo "Host: {$result->host}";
}

$captcha = $client->createCaptcha(
    folderId: 'b1g0ijbfaqsn12345678',
    name: 'my-captcha',
    options: [
        'allowedSites' => ['example.com'],
        'complexity' => 'MEDIUM', // EASY, MEDIUM, HARD
        'preCheckType' => 'CHECKBOX', // CHECKBOX, SLIDER
        'challengeType' => 'IMAGE_TEXT',
    ]
);

echo "Captcha ID: {$captcha->id}";
echo "Client Key: {$captcha->clientKey}";

$captcha = $client->getCaptcha('captcha-id');

echo "Name: {$captcha->name}";
echo "Complexity: {$captcha->complexity}";
echo "Created: {$captcha->createdAt}";

$result = $client->listCaptchas(
    folderId: 'b1g0ijbfaqsn12345678',
    pageSize: 50
);

foreach ($result['captchas'] as $captcha) {
    echo "- {$captcha->name} ({$captcha->id})\n";
}

// Pagination
if ($result['nextPageToken']) {
    $nextPage = $client->listCaptchas(
        folderId: 'b1g0ijbfaqsn12345678',
        pageSize: 50,
        pageToken: $result['nextPageToken']
    );
}

$captcha = $client->updateCaptcha(
    captchaId: 'captcha-id',
    updates: [
        'name' => 'new-name',
        'complexity' => 'HARD',
        'allowedSites' => ['example.com', 'test.com'],
    ]
);

$operation = $client->deleteCaptcha('captcha-id');
echo "Operation ID: {$operation['id']}";

$secretKey = $client->getSecretKey('captcha-id');
echo "Secret Key: {$secretKey->serverKey}";

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use Tigusigalpa\YandexSmartCaptcha\Laravel\Facades\SmartCaptcha;

class SmartCaptchaRule implements Rule
{
    public function passes($attribute, $value): bool
    {
        $result = SmartCaptcha::validate(
            $value,
            config('smartcaptcha.secret_key'),
            request()->ip()
        );
        
        return $result->isValid();
    }
    
    public function message(): string
    {
        return 'Please complete the captcha verification.';
    }
}

$request->validate([
    'smart-token' => ['

use Tigusigalpa\YandexSmartCaptcha\Exceptions\{
    SmartCaptchaException,
    AuthenticationException,
    ValidationException,
    NotFoundException,
    RateLimitException
};

try {
    $result = $client->validate($token, $secret);
} catch (AuthenticationException $e) {
    // Invalid IAM token or secret key
} catch (ValidationException $e) {
    // Invalid request parameters
} catch (NotFoundException $e) {
    // Captcha not found
} catch (RateLimitException $e) {
    // Too many requests
} catch (SmartCaptchaException $e) {
    // Other errors
}

// config/smartcaptcha.php
'logging' => [
    'enabled' => true,
    'channel' => 'stack',
],

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$logger = new Logger('smartcaptcha');
$logger->pushHandler(new StreamHandler('path/to/smartcaptcha.log'));

$client = new SmartCaptchaClient($iamToken, null, $logger);
bash
composer 
bash
php artisan vendor:publish --tag=smartcaptcha-config
bash
git clone https://github.com/tigusigalpa/yandex-smartcaptcha-php.git
cd yandex-smartcaptcha-php