PHP code example of zhylon / captcha-php

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

    

zhylon / captcha-php example snippets


use Zhylon\CaptchaPhp\CaptchaClient;

$client = new CaptchaClient(secretKey: 'your-secret-key');

$result = $client->verify($_POST['captcha_token'] ?? null);

if (! $result->success) {
    // $result->reason is one of: invalid_token, config_error, network_error
    // $result->message contains a human-readable explanation
    abort(403, $result->message);
}

use Zhylon\CaptchaPhp\CaptchaClient;
use Zhylon\CaptchaPhp\CaptchaProxy;

$client = new CaptchaClient(secretKey: 'your-secret-key');
$proxy = CaptchaProxy::forClient($client);

Route::get('captcha/challenge', function () use ($proxy) {
    $response = $proxy->challenge(['sitekey' => request()->get('sitekey')]);

    return response()->json($response->body, $response->status);
});

Route::post('captcha/verify', function () use ($proxy) {
    $response = $proxy->verify(request()->only(['nonce', 'solve_ms', 'token']));

    return response()->json($response->body, $response->status);
});
bash
composer