PHP code example of dario_swain / re-captcha-library

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

    

dario_swain / re-captcha-library example snippets



    use DS\Library\ReCaptcha\Http\Client\Guzzle\GuzzleClient;

    $reCaptchaGuzzleClient = new GuzzleClient(); //Guzzle client will be detected automatically
    
    //Also you can manually create and initialize Guzzle Client
    $guzzle = new \GuzzleHttp\Client($configuration);
    $reCaptchaGuzzleClient = new GuzzleClient($guzzle);

    $reCaptchaClient = new Client('PRIVATE KEY', $reCaptchaGuzzleClient);
    $reCaptchaClient->validate($gResponse);
 php


    $privateKey = 'RECAPTCHA PRIVATE KEY'; //You Google API private key
    $clientIp = $_SERVER['REMOTE_ADDR']; //Client IP Address
    $gReCaptchaResponse = $_POST['g-recaptcha-response']; //Google reCAPTCHA response

    $reCaptchaClient = new Client($privateKey);
    
    try {
        $success = $reCaptchaClient->validate($gReCaptchaResponse, $clientIp);
        
        if ($success) {
            //Submit form
        }
        
    } catch(ValidationException $e) {
        $validationError = $e->getMessage();
    }    

examples/index.php
 php

    class ProxyClient implements ClientInterface
    {
        {@inheritdoc}
        public function send(RequestInterface $request);
        {
            //Your business logic
        }
    }
    
    ...
    
    $proxyHttpClient = new ProxyClient();
    $reCaptchaClient = new Client($privateKey, $proxyHttpClient);
    
	$reCaptchaClient->validate($gReCaptchaResponse, $clientIp);