当我检查 reCaptcha 服务器端时发出警告

Warning when I check the reCaptcha server side

我在服务器端检查 reCAPTCHA 时收到警告。 这是我的错误:

Warning: file_get_contents(https://www.google.com/recaptcha/api/siteverify?secret=mysecretkey&response=03AHJ_VuvejqLNxxSFNgqAH5ma83UyRQkowutkQBVlxMrsvTH_IyVhOyLWNYgoOZQGEDu0Dt6wMo2xCkcsFv44tcRAh1ysPdJvqSraNxD9e422YKjdxV0ngz1_8kz0QarboIMxKwCjcuoQnPLZGRFf9w9it_JrT9BXFh3AXFb3NZgH3bQGJUz_46TgGATy4-UdKLHyWZG3_CXoIsJyp8lJmzqd4vQoHrqJ2WZSQTZpQsKoww7D399xQqwbd2I5KjGkQblfvj3dAKRGz-vCQ_TRlGmU4PmSu9azdWSCocDu2suYdWNaazunvvYV0dPtG7TGS9YmxbZuz5b0YdSTJqHSwhLdF3i8B5apS_KI1F3K45rrEO88-HlNfxUYzAAkm6mxLtSjfzp_73UUtn2o7k0dQUpFaCnwQuv47a41fBNetIERmsXl7nmFGnRb_SOzbKcYaBueNlhvBvYa8p3lRtN1wA2Fuact9kvDB78fDAIZ2oPaeIDj-_Qme17PPFmQKsyh6Yd--pDu2rz0oSIEU5lWrXslVsdLwV89az4ImndilWbRvNFKMcYoKDuTiaXXWTPu50H5m9bLuCAIw-A0JuDZt6aowm4rgh3HxNTlkD5xBi3An-T7A-oPzxk4AStRBEfNTkEiUbvLiigXIdzvoJCNqJHq9ZWKgJ6k5I0Gm_O5dblIb7QAuqB-zBPV5lL-hrESF9bzoXFdn-P8-AVL9Pg1vr8V0PfPOyjLg3-A5dSvi9HT7GIBbkvnu-FXdNCO7RXy-aQqqHRmgs7Tl6-6lStdgSvwr1kIWxrXo9Wry6AzWZ1SdYUPjHMYhxhTmtGVImwHOELTVHwjnXchrRPJym8_8aY2BXfPSlZWBnT8WodHn-liEAP1yDEWKsTjotiUfwM

我理解"invalid-input-secret"因为我在上面的url中使用了"mysecretkey"。问题是"invalid-input-response"。 我在下面粘贴我的代码。

$secretKey = "mysecretkey";
$ip = $_SERVER['REMOTE_ADDR'];
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$_POST["g-recaptcha-response"]."&remoteip=".$ip);
$responseKeys = json_decode($response,true);
if(intval($responseKeys["success"]) !== 1)
{
    echo "you're a robot";
} else {
    echo "form's control";
}

感谢您的帮助。

也许您的 php.ini 中的 allow_url_fopen = Off 阻止了 file_get_contents 打开 URL。

您可以为此使用 cURL,如下所示:

 $fields = array(
        'secret'    =>  "MySecretKey",
        'response'  =>  $captcha,
        'remoteip'  =>  $_SERVER['REMOTE_ADDR']
    );
    $ch = curl_init("https://www.google.com/recaptcha/api/siteverify");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
    $response = json_decode(curl_exec($ch));
    curl_close($ch);

添加

$secretKey = "mysecretkey";
$captcha = $_POST["g-recaptcha-response"]; //Add This Line

并尝试:

$url="https://www.google.com/recaptcha/api/siteverify?secret={$secretkey}&response={$captcha}&remoteip={$ip}";

$options = array(
    'secret'    =>  "MySecretKey",
    'response'  =>  $captcha,
    'remoteip'  =>  $_SERVER['REMOTE_ADDR']
);
$context = stream_context_create( $options );
$responseKeys=json_decode( file_get_contents( $url, FILE_TEXT, $context ) );
if( $responseKeys->success ){
/* everything is ok */
}
else{ 
/* captcha failed */ 
}