1. Go to this page and download the library: Download sunrise/recaptcha 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/ */
sunrise / recaptcha example snippets
use Sunrise\Coder\CodecManager;
use Sunrise\Coder\Codec\UrlEncodedCodec;
use Sunrise\Http\Client\Curl\Client;
use Sunrise\Http\Message\RequestFactory;
use Sunrise\Http\Message\ResponseFactory;
use Sunrise\Hydrator\Hydrator;
use Sunrise\Recaptcha\Dto\RecaptchaVerificationRequest;
use Sunrise\Recaptcha\RecaptchaVerificationClient;
use Sunrise\Recaptcha\RecaptchaVerificationConfiguration;
// https://developers.google.com/recaptcha/docs/faq
$privateKey = '6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe';
$client = new RecaptchaVerificationClient(
verificationConfiguration: new RecaptchaVerificationConfiguration(privateKey: $privateKey),
httpRequestFactory: new RequestFactory(),
httpClient: new Client(new ResponseFactory()),
codecManager: new CodecManager([new UrlEncodedCodec()]),
hydrator: new Hydrator(),
);
$userToken = '';
$clientResponse = $client->sendRequest(new RecaptchaVerificationRequest(userToken: $userToken));
if ($clientResponse->success) {
// Challenge passed
} else {
// Challenge failed
}
use Sunrise\Http\Router\Annotation\Consumes;
use Sunrise\Http\Router\Annotation\Middleware;
use Sunrise\Http\Router\Annotation\PostApiRoute;
use Sunrise\Http\Router\Annotation\RequestBody;
use Sunrise\Http\Router\MediaType;
use Sunrise\Recaptcha\Integration\Router\Middleware\RecaptchaChallengeMiddleware;
final class AuthController
{
#[PostApiRoute('api.login', '/api/login')]
#[Consumes(MediaType::JSON)]
#[Middleware(RecaptchaChallengeMiddleware::class)]
public function login(#[RequestBody] LoginRequest $loginRequest): void
{
}
}
use SensitiveParameter;
use Sunrise\Recaptcha\Integration\Validator\Constraint\RecaptchaChallenge;
use Symfony\Component\Validator\Constraints as Assert;
final readonly class UserLoginRequest
{
public function __construct(
#[Assert\NotBlank]
#[Assert\Email]
#[SensitiveParameter]
public string $email,
#[Assert\NotBlank]
#[SensitiveParameter]
public string $password,
#[Assert\NotBlank(message: 'Prove that you are not a robot.')]
#[RecaptchaChallenge]
#[SensitiveParameter]
public string $recaptcha,
) {
}
}
use DI\ContainerBuilder;
use Sunrise\Recaptcha\RecaptchaVerificationClientInterface;
$containerBuilder = new ContainerBuilder();
$containerBuilder->addDefinition(__DIR__ . '/../vendor/sunrise/recaptcha/resources/definitions/recaptcha_verification.php');
// If Sunrise Router is used
$containerBuilder->addDefinitions(__DIR__ . '/../vendor/sunrise/recaptcha/resources/definitions/integration/router/middleware/recaptcha_challenge_middleware.php');
// If Symfony Validator is used
$containerBuilder->addDefinition(__DIR__ . '/../vendor/sunrise/recaptcha/resources/definitions/integration/validator/constraint/recaptcha_challenge_validator.php');
$container = $containerBuilder->build();
// See above for usage examples.
$recaptchaClient = $container->get(RecaptchaVerificationClientInterface::class);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.