dario_swain/re-captcha-library
PHP backend client for Google reCAPTCHA v2. Validate the user’s g-recaptcha-response token against Google using your secret key (optionally passing the client IP) to confirm form submissions and block bots. Composer-installable and lightweight.
Pros:
ClientInterface), enabling advanced use cases like proxying, request logging, or retry mechanisms. This is particularly useful for debugging or integrating with enterprise-grade HTTP clients.Cons:
laravel-recaptcha.Laravel Integration:
$this->app->singleton(ReCaptcha::class, function ($app) {
return new \DS\Library\ReCaptcha\Client(config('services.recaptcha.secret'));
});
use DS\Library\ReCaptcha\ValidationException;
class ReCaptchaRule implements RuleInterface {
public function passes($attribute, $value) {
$client = app(ReCaptcha::class);
try {
return $client->validate($value, request()->ip());
} catch (ValidationException $e) {
return false;
}
}
}
/contact, /register).@recaptcha).Frontend:
api.js) and the g-recaptcha div. No backend for v3/invisible CAPTCHA means additional work if migrating later.Compatibility Risks:
error_reporting) or type system changes (e.g., array vs. list).Illuminate\Http\Client).Illuminate\Http\Client) may not align with the package’s PSR-7 expectations.mockery to test validation logic.HttpClient (Guzzle-based) or symfony/http-client for PSR-7 compliance. The package’s Guzzle support ensures seamless integration.ReCaptchaRule). Example:
public function rules() {
return [
'g-recaptcha-response' => ['required', new ReCaptchaRule],
];
}
public function handle(Request $request, Closure $next) {
$response = $request->input('g-recaptcha-response');
$success = app(ReCaptcha::class)->validate($response, $request->ip());
if (!$success) abort(422, 'CAPTCHA verification failed');
return $next($request);
}
$this->app->bind(ReCaptcha::class, function ($app) {
$client = new \DS\Library\ReCaptcha\Client(config('services.recaptcha.secret'));
// Custom HTTP client (e.g., Guzzle with middleware)
$httpClient = new \DS\Library\ReCaptcha\Http\Client\Guzzle\GuzzleClient(
new \GuzzleHttp\Client(['timeout' => 2])
);
$client->setHttpClient($httpClient);
return $client;
});
.env and publish a config file:
RECAPTCHA_SECRET=your_secret_key
RECAPTCHA_SITE_KEY=your_site_key
// config/recaptcha.php
return [
'secret' => env('RECAPTCHA_SECRET'),
'site_key' => env('RECAPTCHA_SITE_KEY'),
];
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<div class="g-recaptcha" data-sitekey="{{ config('recaptcha.site_key') }}"></div>
g-recaptcha-response field is submitted with the form.How can I help you explore Laravel packages today?