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}";
}
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
}
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);