/recaptcha

An easy-to-use reCAPTCHA package

Primary LanguagePHPMIT LicenseMIT

reCAPTCHA

recaptcha

An easy-to-use reCAPTCHA package.

use Vinkla\Recaptcha\Recaptcha;

// Create a new recaptcha instance.
$recaptcha = new Recaptcha('your-secret-key');

// Verify the recaptcha response.
$recaptcha->verify('g-recaptcha-response');

Build Status StyleCI Coverage Status Latest Version License

Installation

reCAPTCHA is decoupled from any library sending HTTP requests (like Guzzle), instead it uses an abstraction called HTTPlug which provides the http layer used to send requests to exchange rate services. This gives you the flexibility to choose what HTTP client and PSR-7 implementation you want to use.

Read more about the benefits of this and about what different HTTP clients you may use in the HTTPlug documentation. Below is an example using Guzzle 6:

$ composer require vinkla/recaptcha php-http/message php-http/guzzle6-adapter

Usage

First you'll need to add the Google reCAPTCHA script to your view templates just before the closing </head> tag.

If you want to use another language you made update the hl query parameter value.

<script src="https://google.com/recaptcha/api.js?hl=en"></script>

To display the reCAPTCHA field in your form you need to add the snippet below.

Remember to replace your-site-key with your actual reCAPTCHA site key.

<div class="g-recaptcha" data-sitekey="your-site-key"></div>

Then create a new Vinkla\Recaptcha\Recaptcha instance.

use Vinkla\Recaptcha\Recaptcha;

$recaptcha = new Recaptcha('your-secret-key');

To validate a response from the form you can use the verify() method.

use Vinkla\Recaptcha\RecaptchaException;

try {
    $recaptcha->verify('g-recaptcha-response');
} catch (RecaptchaException $e) {
    // If the verification fails.
}

If you want to to read more about reCAPTCHA, I'd suggest you head over to the official documentation.

License

MIT © Vincent Klaiber