File: //home/artinside/sites.artinside.com.br/mainpro/source/Support/RecaptchaV2.php
<?php
/**
* Created by PhpStorm.
* User: sergiohidalgojunior
* Date: 2019-09-17
* Time: 09:23
*/
namespace Source\Support;
/**
* Class RecaptchaV2
* @package Source\Support
*/
class RecaptchaV2
{
/**
* ReCAPTCHA URL verifying
*
* @var string
*/
const VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify';
/**
* Private key
*
* @var string
*/
private $secretKey;
/** @var Message */
private $message;
/**
* Initialize site and secret keys
*
* @param string $secretKey Secret key from ReCaptcha dashboard
* @return void
*/
public function __construct($secretKey = "6Letp7MlAAAAAAdb4sa8R1-Ijnzi6VK5DK7pz-mg")
{
$this->setSecretKey($secretKey);
$this->message = new Message();
}
/**
* Set secret key
*
* @param string $key
* @return object
*/
public function setSecretKey($key)
{
$this->secretKey = $key;
return $this;
}
/**
* Checks the code given by the captcha
*
* @param string $response Response code after submitting form (usually $_POST['g-recaptcha-response'])
* @return bool
*/
public function isValid($response)
{
if (empty($this->secretKey))
return false;
if (empty($response)) {
$this->message->error("Não clicou em não sou um robo");
return false;
}
$curl = curl_init();
curl_setopt_array($curl,[
CURLOPT_URL => self::VERIFY_URL,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => [
'secret' => $this->secretKey,
'response' => $response ?? ''
]
]);
$response = curl_exec($curl);
curl_close($curl);
$responseObject = json_decode($response);
return $responseObject->success;
}
}