Recaptcha V2返回false PHP

时间:2018-05-25 10:20:13

标签: php forms recaptcha verification

我正在尝试以PHP格式实施Google ReCaptcha V2。

这是我的代码:

    $arrContextOptions=array(
    "ssl"=>array(
        "verify_peer"=>false,
        "verify_peer_name"=>false,
    ),
);

if($_SERVER["REQUEST_METHOD"] === "POST")
    {
        //form submitted

        //check if other form details are correct

        //verify captcha
        $recaptcha_secret = "";
        $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$_POST['g-recaptcha-response'], false, stream_context_create($arrContextOptions));
        $response = json_decode($response, true);
        if($response["success"] === true)
        {
            echo "Logged In Successfully";
        }
        else
        {
            echo "You are a robot";
        }
    }

?>

当我提交表单时,它总是返回

  

你是一个机器人

我的公钥是正确的,我的私钥也是。

我不知道我做错了什么?

我是localhost。

感谢。

1 个答案:

答案 0 :(得分:1)

刚刚整合了2天前来自谷歌的V2回收

请尝试下面的代码,明确是否正在解决您的问题:

我可以看到你做了file_get_contents,我认为这是你的问题,你必须发帖,请使用下面的代码

if($_SERVER["REQUEST_METHOD"] === "POST"){
    // prepare post variables
    $post = [
        'secret' => $secret,
        'response' => $_POST['g-recaptcha-response'],
        'remoteip'   => 'is optional, but i pass it',
    ];

    $ch = curl_init('https://www.google.com/recaptcha/api/siteverify');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

    $response = curl_exec($ch);
    curl_close($ch);

    var_dump($response);
    $response = json_decode($response, true);

    // check result
    if(isset($response['success']) && $response['success'] == true){
        echo "Logged In Successfully";
    }else{
        echo "You are a robot";
    }
}