PHP code example of geggleto / psr7-recaptcha

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

    

geggleto / psr7-recaptcha example snippets


$container[Recaptcha::class] = new \ReCaptcha\ReCaptcha($key);

$recaptcha = new \Geggleto\Service\Captcha($container[Recaptcha::class]);

//As Middleware
$app->post('/login', 'Auth:login')->add($recaptcha);

//Anywhere else
$recaptcha->verify($response, $ip);


// Slim 3 Example
// Container File
//...
use Geggleto\Service\Captcha;
use ReCaptcha\ReCaptcha;
//...
    Captcha::class => function ($c) {
        return new Captcha($c[ReCaptcha::class]);
    },
    ReCaptcha::class => function ($c) {
        return new ReCaptcha($c['captcha']['key']);
    },
    'errorHandler' => function ($c) { //CUSTOM Error Handler
        return function (\Slim\Http\Request $request, \Slim\Http\Response $response, $exception) use ($c) {
            return $c['view']->render($response, "errors/servererror.twig", ["exception" => $exception]);
        };
    }    
//...

//In routes.php

//Grab the instance from the Container
$captcha = $app->getContainer()->get(Captcha::class);

//Applying it to routes
$app->post('/signin', Home::class.':processLogin')->add($captcha);
$app->post('/forgot', Home::class.':processForgot')->add($captcha);
$app->post('/recover/{key}', Home::class.':processRecover')->add($captcha);