Google reCaptcha 错误

Google reCaptcha error

代码:

if(isset($_POST['submit']) && !empty($_POST['submit'])) {
    if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){
        //your site secret key
        $secret = 'KEY';
        //get verify response data
        $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
        $responseData = json_decode($verifyResponse);
        echo var_dump($responseData);
        if($responseData->success){

            //contact form submission code

            $connection = mysqli_connect("localhost","user","pass");
            if (!$connection) {
            die("Database connection failed: " . mysqli_error());
            }
            $db_select = mysqli_select_db($connection, "database");
            if (!$db_select) {
                die("Database selection failed: " . mysqli_error());
            }
            $email = $_POST['email'];
            $regHour = date('d-m-Y H:i');
            $order = "INSERT INTO `database`.`pre_register` (id, email, register_time) VALUES ('', '$email', '$regHour')";
            $result = mysqli_query($connection, $order);    
            if($result){
            echo "OK";
            }
            else{
            echo "error";
            }
        else    {
     echo 'Please check the reCAPTCHA box.';
     }

}

它不起作用。如果我检查 reCaptcha 并完成电子邮件,它会给我 "Error"。如果我完成输入电子邮件但不检查 reCaptcha,它会给我错误 "Please check the reCAPTCHA box."

如果能提供一点帮助,我将不胜感激。我不明白为什么它会给我错误信息。数据库连接参数没问题。我仔细检查了它们。

echo var_dump($responseData); 显示消息:"NULL Please check the reCAPTCHA box."

我认为问题在于您缺少底部的 }(或 2 个)括号。

更新以下位

if($result){
            echo "OK";
            }
            else{
            echo "error";
            }
        else    {
     echo 'Please check the reCAPTCHA box.';
     }

}

以下

    if($result){
                echo "OK";
                }
                else{
                echo "error";
                }
        } else    {
            echo 'reCAPTCHA is invalid';
        }
    }else {
        echo 'Please check the reCAPTCHA box.';
    }
}

另外请更新 file_get_cotents() 来自

的请求
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
    $responseData = json_decode($verifyResponse);

curl 请求

    $url = 'https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response'];

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, TRUE); 
$curlData = curl_exec($curl);

curl_close($curl);

$responseData = json_decode($curlData, TRUE);

完整代码(带有一些格式)

if(isset($_POST['submit']) && !empty($_POST['submit'])) {
    if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) {
        //your site secret key
        $secret = 'KEY';
        //get verify response data
        // $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);

        $url = 'https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response'];

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($curl, CURLOPT_TIMEOUT, 15);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, TRUE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, TRUE); 
        $curlData = curl_exec($curl);

        curl_close($curl);

        $responseData = json_decode($curlData, TRUE);
        if($responseData['success'] == 'true') {

        // $responseData = json_decode($verifyResponse);
        // if($responseData->success) {

            //contact form submission code

            $connection = mysqli_connect("localhost","user","pass");
            if (!$connection) {
                die("Database connection failed: " . mysqli_error());
            }

            $db_select = mysqli_select_db($connection, "database");
            if (!$db_select) {
                die("Database selection failed: " . mysqli_error());
            }

            $email = $_POST['email'];
            $regHour = date('d-m-Y H:i');
            $order = "INSERT INTO `database`.`pre_register` (id, email, register_time) VALUES ('', '$email', '$regHour')";
            $result = mysqli_query($connection, $order);    
            if($result){
                echo "OK";

            } else {
                echo "error";
            }

        } else {
            echo 'reCAPTCHA is invalid';
        }
    }else {
        echo 'Please check the reCAPTCHA box.';
    }
}