PHP code example of nietonfir / google-recaptcha

1. Go to this page and download the library: Download nietonfir/google-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/ */

    

nietonfir / google-recaptcha example snippets




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());
}
bash
# Install Composer
curl -sS https://getcomposer.org/installer | php