ReCAPTCHA is a free CAPTCHA service that protect websites from spam and abuse. This library aims to providate an alternative to the official ReCAPTCHA library for verifying a users "No CAPTCHA reCAPTCHA" response. Internally it uses Guzzle for communicating with the ReCAPTCHA API.
The recommended way to install GoogleReCaptcha is through Composer.
# Install Composer
curl -sS https://getcomposer.org/installer | php
Next, run the Composer command to install the latest stable version of GoogleReCaptcha:
composer require "nietonfir/google-recaptcha"
Or add GoogleReCaptcha in your composer.json
"require": {
"nietonfir/google-recaptcha": "~0.0"
}
and tell Composer to install the library:
composer update "nietonfir/google-recaptcha"
After installing, you need to require Composer's autoloader:
require 'vendor/autoload.php';
A sample validation client could look like the following:
use GuzzleHttp\Client;
use Nietonfir\Google\ReCaptcha\ReCaptcha;
use Nietonfir\Google\ReCaptcha\Api\RequestData,
Nietonfir\Google\ReCaptcha\Api\ResponseFactory;
$requestData = new RequestData(
'YOUR_API_SECRET_HERE', // secret
$_POST['g-recaptcha-response'], // user response
$_SERVER['REMOTE_ADDR'] // end user IP
);
$reCaptcha = new ReCaptcha(new Client(), new ResponseFactory());
$response = $reCaptcha->processRequest($requestData);
if ($response->isValid()) {
// check the hostname if "Domain Name Validation" is turned off
// if($_SERVER['SERVER_NAME'] === $response->getHostName()) { … }
echo 'I\'m not a robot';
} else {
var_dump($response->getErrors());
}