2017-02-19 136 views
2

嗨,大家好,我對google 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 
} 

的print_r的輸入是:陣列([成功] => [錯誤碼] =>數組([0] =>缺少輸入響應))

如何解決這個問題?

感謝您的回答

回答

4

請注意:g-recaptcha-respone = g-recaptcha-response

enter image description here

enter image description here

谷歌reCatcha API,您可能需要指定其他參數的file_get_contents函數調用,專門設置上下文選項SSL(如果網站具有SSL)。

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

$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 
} 

至少在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 

和您的憑證檔案錯誤和路徑將是

capath=/etc/ssl/certs/ 
cafile=/etc/ssl/certs/cacert.pem 
+0

我的網站上沒有SSL,那麼放入$ options = array是什麼?或到$上下文? – Forlis

+1

@Forlis:看到我的更新,你是否缺少's'?在'g-recaptcha-response'中 –

+1

是的,這是工作。太多了! – Forlis

0

即時通訊無法發表評論,所以我即將在這裏回答。我複製了我的完美代碼。和btw,$ _POST ['g-recaptcha-respone'],你確定你的輸入名字是'g-recaptcha-respone'?

$secret = 'SECRET-KEY'; 
$response = $_POST['g-recaptcha-response']; 
$ip = $_SERVER['REMOTE_ADDR']; 

$dav = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$response."&remoteip=".$ip); 

$res = json_decode($dav,true); 

if($res['success']) { 
    die(json_encode(0)); 
} else { 
    die(json_encode(1)); 
} 
0

在我來說,我需要增加兩個額外的參數( '', '&'):

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