PHP Verification Package
The PHP package alancaptcha/php provides a simple way to ìnteract with the HTTP API when using PHP.
It can be installed via Composer:
composer require alancaptcha/php
The class AlanCaptcha\Php\AlanApi provides the methods:
challengeto retrieve a new challengewidgetValidateto validate if the solutions provided directly by the widget are correctchallengeValidateto validate if the solutions are correcthealthto check if the service is available
To use any of the methods, you need to instantiate the AlanApi class and call the desired method with the required parameters. This example shows how to use the widgetValidate method:
use AlanCaptcha\Php\AlanApi;
function exampleFunction(string $alanSolutionFieldValue): bool {
$api = new AlanApi();
return $api->widgetValidate(\getenv('ALAN_API_KEY'), $alanSolutionFieldValue);
}
challenge(...): string|array
challenge(
string $siteKey,
string $monitorTag = 'general',
int $numberOfPuzzles = 0,
int $difficulty = 0,
int $rounds = 0,
int $numberOfChallenges = 0
): string|array
Used to retrieve a new challenge.
| Parameter | Required | Description |
|---|---|---|
$siteKey | mandatory | The SiteKey you received from registering your site. |
$monitorTag | optional | A previously created monitorTag. |
$numberOfPuzzles | optional | Integer. If allowed, generates a challenge with this number of puzzles. The default configuration can always be increased. |
$difficulty | optional | Integer. If allowed, set the difficulty for generated puzzles. The default configuration can always be increased. |
$rounds | optional | Integer. If allowed, set the number of rounds to compute a hash needed for a solution. The default configuration can always be increased. |
$numberOfChallenges | optional | Integer between 1 and 1000. If greater than 1, returns a json array with given number of challenge JWT's |
if $numberOfChallenges is greater than 1, the method returns an array of challenge JWTs, otherwise a single challenge JWT string.
widgetValidate(...): bool
widgetValidate(string $apiKey, string $alanSolutionsFormField): bool
Validate if the solutions provided directly by the widget are correct.
Returns true if valid, false if invalid, Exception on malformed Json or on service or network disruption.
| Parameter | Description |
|---|---|
$apiKey | The private ApiKey |
$alanSolutionsFormField | Json encoded array containing the JWT and corresponding solutions ['jwt' => JWT, 'solutions' => [alan solutions...]] |
challengeValidate(...): bool
challengeValidate(string $apiKey, string $jwt, array $puzzleSolutions): bool
Validate if parsed solutions are correct.
Returns true if valid, false if invalid, Exception on service or network disruption.
| Parameter | Description |
|---|---|
$apiKey | The private ApiKey |
$jwt | The JWT related to the solutions |
$puzzleSolutions | Solutions computed for the given JWT |
health(): bool
Check if the AlanCaptcha service is available.
Returns true if available, false if not, Exception on network disruption.
PSR Middleware
Additionally, the package provides a PSR Middleware to handle ALAN Captcha Validation automatically via HTTP request headers or during form data post. It can be used with any PSR compatible frameworks like Slim, Flow, etc.
use AlanCaptcha\Php\Middleware\AlanCaptchaMiddleware;