<?php
define("RECAPTCHA_V3_SECRET_KEY", '6LdXALciAAAAABvvpXJpN51IUrr5vWh1ndgdC6M6');
$token = $_POST['token'];
$action = $_POST['action'];
 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://www.google.com/recaptcha/api/siteverify");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('secret' => RECAPTCHA_V3_SECRET_KEY, 'response' => $token)));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$arrResponse = json_decode($response, true);
 

$dom = new DOMDocument();
     $dom->encoding = 'utf-8';
     $dom->xmlVersion = '1.0';
     $dom->formatOutput = true;

$response=$dom->appendChild($dom->createElement("response"));
     $result=$response->appendChild($dom->createElement("success",$arrResponse["success"])); 
     $result=$response->appendChild($dom->createElement("action",$arrResponse["action"])); 
     $result=$response->appendChild($dom->createElement("score",$arrResponse["score"])); 
if($arrResponse["success"] == '1' and $arrResponse["action"] == $action and $arrResponse["score"] >= 0.5)
{
     // Si entra aqui, es un humano, puedes procesar el formulario
     // 	echo "ok!, eres un humano";
     $result=$response->appendChild($dom->createElement("result","OK")); 
     $result->setAttribute("id","recaptcha");
     $captcha_ok=1;
}else{
     // Si entra aqui, es un robot....
     // 	echo "Lo siento, parece que eres un Robot";
     $result=$response->appendChild($dom->createElement("result","Error")); 
     $result->setAttribute("id","recaptcha");
     $captcha_ok=2;
}

$recaptcha_v3=$dom->saveXML();
echo $recaptcha_v3;
// return $dom->saveXML();

?>
