I have a problem with google reCaptcha.
Here is my php code:
$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
}
input of print_r is: Array ( [success] => [error-codes] => Array ( [0] => missing-input-response ) )
How to solve this problem?
Thanks for answers
This error happened to me because I had two instances of the ReCaptcha element on my page (one for mobile views, one for desktop). As soon as I removed one of them this error stopped.
Please note : g-recaptcha-respone
!= g-recaptcha-response
Google reCatcha API you might need to specify additional parameters to the file_get_contents
function call, setting the context options specifically for SSL (If site has 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
}
At least on ubuntu - If site has 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
and your cafile and path will be
capath=/etc/ssl/certs/
cafile=/etc/ssl/certs/cacert.pem
In my case I needed to add two extra parameters ('', '&'
) in this call:
http_build_query(array(
'secret' => $secret,
'response' => $response,
'remoteip' => $remoteip
), '', '&');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With