Recaptcha缺失 - 输入 - 响应

For*_*lis 8 php recaptcha

嗨伙计们我有谷歌reCaptcha的问题.

这是我的PHP代码:

$secret = 'SECRET_KEY';
            $response = $_POST['g-recaptcha-respone'];
            $remoteip = $_SERVER['REMOTE_ADDR'];

            $url = "https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$response&remoteip=$remoteip";
            $result_json = file_get_contents($url);
            $resulting = json_decode($result_json, true);
            print_r($resulting);

if($resulting['success']) {
  //Success
}
Run Code Online (Sandbox Code Playgroud)

print_r的输入是:Array([success] => [error-codes] => Array([0] => missing-input-response))

如何解决这个问题呢?

谢谢你的回答

Aks*_*gde 10

请注意:g-recaptcha-respone!=g-recaptcha-response

在此输入图像描述

在此输入图像描述

Google reCatcha API您可能需要为file_get_contents函数调用指定其他参数,专门为SSL设置上下文选项(如果站点具有SSL).

// If submitted check response
if ($_POST["g-recaptcha-response"]) {

// Input data
$secret = 'SECRET_KEY';
$response = $_POST['g-recaptcha-response'];
$remoteip = $_SERVER['REMOTE_ADDR'];

$url = "https://www.google.com/recaptcha/api/siteverify";

$post_data = http_build_query(
    array(
        'secret' => $secret,
        'response' => $response,
        'remoteip' => $remoteip
    )
);  

$options=array(

    // If site has SSL then
    'ssl'=>array(

        // In my case its /etc/ssl/certs/cacert.pem

        'cafile'            => '/path/to/cacert.pem',
        'verify_peer'       => true,
        'verify_peer_name'  => true,
    ),

   'http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $post_data
    )
);

$context = stream_context_create( $options );   

$result_json = file_get_contents( $url, false, $context );
$resulting = json_decode($result_json, true);

if($resulting['success']) {
    //Success
} else {
     // action for no response 
}
Run Code Online (Sandbox Code Playgroud)

至少在ubuntu上 - 如果站点有SSL

cd /usr/local/share/ca-certificates 
sudo curl http://curl.haxx.se/ca/cacert.pem -o cacert.crt 
sudo update-ca-certificates
sudo update-ca-certificates –fresh
Run Code Online (Sandbox Code Playgroud)

你的咖啡馆和小路将是

capath=/etc/ssl/certs/
cafile=/etc/ssl/certs/cacert.pem
Run Code Online (Sandbox Code Playgroud)

  • @Forlis:查看我的更新,您是否缺少“s”?在“g-recaptcha-response”中 (2认同)

Mar*_*iba 5

就我而言,我需要'', '&'在此调用中添加两个额外的参数 ( ):

http_build_query(array(
    'secret' => $secret,
    'response' => $response,
    'remoteip' => $remoteip
), '', '&');
Run Code Online (Sandbox Code Playgroud)