无法从 ReCaptchaResponse 获取错误代码

Unable to get error codes from ReCaptchaResponse

我目前在我的网站上使用 here 到 运行 reCaptcha 可用的 reCaptcha 库,它使用 LAMP 2.2.22 版和 PHP 5.4 版。 39-0+deb7u2,在 Debian 6.5 上。我的PHP代码如下

// Recaptcha check
$secret = "SomeAlphaNumericGibberish";
$response = null;
$reCaptcha = new ReCaptcha($secret);
if ($reCaptcha ==NULL){
    echo "Null pointer "  . "<br>";
} else{
    echo "Valid pointer "  . "<br>";
}
    if ($_POST["g-recaptcha-response"]) {
    echo "Remote IP: ". $_SERVER["REMOTE_ADDR"]  . "<br>";
    $response = $reCaptcha->verifyResponse(
        $_SERVER["REMOTE_ADDR"],
        $_POST["g-recaptcha-response"]
    );
}
if ($response != null && $response->success) {
    echo "Welcome " . $_POST["username"] . "!" . "<br>";
} else if ($response == null) {echo "Null pointer "  . "<br>";}
else {
    echo "Bad response"  . "<br>";
      foreach ($response->getErrorCodes() as $code) {
            echo  $code . "<br>";
     }
   echo "End of error codes"  . "<br>";
}

输出结果如下

Valid pointer
Remote IP: 192.168.1.4
Bad response

但是,之后我没有得到任何输出。我运行

sudo cat /var/log/apache2/error.log

得到了

PHP Fatal error:  Call to undefined method ReCaptchaResponse::getErrorCodes()

我找不到关于此错误消息的任何讨论。

根本不需要使用那个库。它增加了不必要的重量。

假设您使用的是最新的 recaptcha js 库并且您的 html 已配置,这将有效:

$recaptchasecret = 'adslfjasdlf';

$recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify?secret='.
    $recaptchasecret.'&response='.$_POST['g-recaptcha-response'].'&remoteip='.$_SERVER['REMOTE_ADDR'];

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => $recaptcha_url,
    CURLOPT_USERAGENT => 'MyWebsite.Com'
));
$response = curl_exec($curl);
curl_close($curl);
unset($curl);

$auth = json_decode($response,false);
if(!$auth->success){
    throw new exception('Captcha Failed!');
}