PHP - 验证 recaptcha v2 响应

PHP - Validate the recaptcha v2 response

#form.php

if(isset($_POST['g-recaptcha-response'])){
    $captcha=$_POST['g-recaptcha-response'];
    $captcha=$_GET["g-recaptcha-response"];
    $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=__1234__&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);

    echo $response;

Returns:

Notice: Undefined index: g-recaptcha-response in /var/www/clients/qmax/app/controllers/job_application_controller.php on line 114 { "success": false, "error-codes": [ "missing-input-response" ] }

echo $response->success;

Returns:

Notice: Trying to get property of non-object in /var/www/clients/qmax/app/controllers/job_application_controller.php on line 119

我只需要获取 "success" 对象,然后我就可以测试它是 True 还是 False,我就完成了。

删除这一行:

$captcha = $_GET["g-recaptcha-response"];

然后你需要用 Google 解码 json 像这样:

$g_response = json_decode($response);

然后用if/else检查一下:

if ($g_response->success === true) echo "success!";