Skip to main content

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:

  • challenge to retrieve a new challenge
  • widgetValidate to validate if the solutions provided directly by the widget are correct
  • challengeValidate to validate if the solutions are correct
  • health to 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.

ParameterRequiredDescription
$siteKeymandatoryThe SiteKey you received from registering your site.
$monitorTagoptionalA previously created monitorTag.
$numberOfPuzzlesoptionalInteger. If allowed, generates a challenge with this number of puzzles. The default configuration can always be increased.
$difficultyoptionalInteger. If allowed, set the difficulty for generated puzzles. The default configuration can always be increased.
$roundsoptionalInteger. If allowed, set the number of rounds to compute a hash needed for a solution. The default configuration can always be increased.
$numberOfChallengesoptionalInteger 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.

ParameterDescription
$apiKeyThe private ApiKey
$alanSolutionsFormFieldJson 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.

ParameterDescription
$apiKeyThe private ApiKey
$jwtThe JWT related to the solutions
$puzzleSolutionsSolutions 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;