Google reCaptcha with php validation

2.7k views Asked by At

i'm trying to use reCaptcha v2.0 with php, in the server verification i use this code:

if(isset($_POST['Direccion_Email']) AND ($_POST['enviar'])) {
    if(isset($_POST['g-recaptcha-response'])){
    $mensaje_error = "";
    include 'config-formulario.php';
    require_once __DIR__ . '/recaptcha-master/src/autoload.php';
    $recaptcha = new \ReCaptcha\ReCaptcha($secret);
    $resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
if (!$resp->isSuccess()) {
        $mensaje_error .= "Control Anti SPAM no es válido <br />";
        $errors = $resp->getErrorCodes();
    } else {
        //DO SOMETHING;
    }

But when i try to send a simple contact me form with name, email and comments, it's return this warnings:

Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in /home/diego/www/systec/scripts/recaptcha-master/src/ReCaptcha/RequestMethod/Post.php on line 68

Warning: file_get_contents(): Failed to enable crypto in /home/diego/www/systec/scripts/recaptcha-master/src/ReCaptcha/RequestMethod/Post.php on line 68

Warning: file_get_contents(https://www.google.com/recaptcha/api/siteverify): failed to open stream: operation failed in /home/diego/www/systec/scripts/recaptcha-master/src/ReCaptcha/RequestMethod/Post.php on line 68

I'm testing this on localhost.

Any suggestions.

Thanks.

2

There are 2 answers

3
Diego On BEST ANSWER

The answer from @jww works form me:

For step (1), see How do you sign Certificate Signing Request with your Certification Authority?. For (2), see How to create a self-signed certificate with openssl?. The latter also shows you how to create a signing request (as opposed to a self signed certificate). They are the same steps - the only thing that differs is the lack of the -x509 option for a signing request.

0
Nabil Mohammed Nalakath On

Change

$verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");

To

$ch = curl_init("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$verify = curl_exec($ch);