Recaptcha Not Verifying with file_get_contents

Asked
Active3 hr before
Viewed126 times

8 Answers

90%

Meta Stack Overflow ,Stack Overflow en español,Stack Overflow em Português, Stack Overflow Public questions & answers

The reCaptcha documentation specifically specifies that the parameters for the request to https://www.google.com/recaptcha/api/siteverify must be sent via POST. You can use CURL for this.

$ch = curl_init();

curl_setopt_array($ch, [
   CURLOPT_URL => 'https://www.google.com/recaptcha/api/siteverify',
   CURLOPT_POST => true,
   CURLOPT_POSTFIELDS => [
      'secret' => $secretKey,
      'response' => $captcha,
      'remoteip' => $_SERVER['REMOTE_ADDR']
   ],
   CURLOPT_RETURNTRANSFER => true
]);

$output = curl_exec($ch);
curl_close($ch);

$json = json_decode($output);

// check response...
load more v
88%

This worked well for me. I had issues with using file_get_contents on a certain server, possibly due to permissions and access, so good ol' curl did the trick., Instantly share code, notes, and snippets. ,This is a much faster, universal and more flexible solution.,However, you might want to replace that with empty($result->success) so it won't throw you E_NOTICE when request failed and json_decode returned false.

Cloudflare users should add this.

if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
   $_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
}
load more v
72%

The reCaptcha documentation specifically specifies that the parameters for the request to https://www.google.com/recaptcha/api/siteverify must be sent via POST. You can use CURL for this.,Yemiez answer (just got me at the corner) is better at handling the response part, by using the json_decode function.,Don't use file_get_contents. Google suggests using POST requests. You may use something in the lines of the following

The reCaptcha documentation specifically specifies that the parameters for the request to https://www.google.com/recaptcha/api/siteverify must be sent via POST. You can use CURL for this.

$ch = curl_init();

curl_setopt_array($ch, [
   CURLOPT_URL => 'https://www.google.com/recaptcha/api/siteverify',
   CURLOPT_POST => true,
   CURLOPT_POSTFIELDS => [
      'secret' => $secretKey,
      'response' => $captcha,
      'remoteip' => $_SERVER['REMOTE_ADDR']
   ],
   CURLOPT_RETURNTRANSFER => true
]);

$output = curl_exec($ch);
curl_close($ch);

$json = json_decode($output);

// check response...
load more v
65%

We detected that your site is not verifying reCAPTCHA solutions. This is required for the proper use of reCAPTCHA on your site. Please see our developer site for more information.,I managed to fix it. It was due to my system not being able to verify the Google certificate.,Apparently my OJS stopped verifying the information. What could have caused that? And what can I do about that?,I had to point the php-openssl to the correct location of “cacert.pem”.

[Mon Feb 19 09: 39: 08.382556 2018][: error][pid 780][client 200.145 .66 .44: 2236] ojs2 has produced an error\ n Message: WARNING: file_get_contents(): Unable to find the wrapper & quot;
https & quot; - did you forget to enable it when you configured PHP ? \n In file : /usr/home / seer / public_html / lib / pkp / lib / recaptcha / recaptchalib.inc.php\ n At line: 198\ n Stacktrace: \n Server info: \n OS: FreeBSD\ n PHP Version: 5.6 .33\ n Apache Version: Apache / 2.4 .29(FreeBSD) PHP / 5.6 .33\ n DB Driver: mysql\ n DB server version: 5.1 .66, referer: http: //seer.fclar.unesp.br/alfa/user/registerUser
   [Mon Feb 19 09: 39: 08.382686 2018][: error][pid 780][client 200.145 .66 .44: 2236] ojs2 has produced an error\ n Message: WARNING: file_get_contents(https: //www.google.com/recaptcha/api/siteverify): failed to open stream: No such file or directory\n  In file: /usr/home/seer/public_html/lib/pkp/lib/recaptcha/recaptchalib.inc.php\n  At line: 198\n  Stacktrace: \n  Server info:\n   OS: FreeBSD\n   PHP Version: 5.6.33\n   Apache Version: Apache/2.4.29 (FreeBSD) PHP/5.6.33\n   DB Driver: mysql\n   DB server version: 5.1.66, referer: http://seer.fclar.unesp.br/alfa/user/registerUser
load more v
75%

Some servers disable the use of file_get_contents, in other words, google has blocked any access from file_get_contents. You can use PHP's cURL to bypass the lock. In case, your code would look like this:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$captcha&remoteip$ip");
curl_setopt($ch, CURLOPT_HEADER, 0);


$rps = curl_exec($ch);

//print da resposta do google
echo "
<pre>"
print_r($rps);

curl_close($ch);
40%

Seems there’s may be a file_get_contents in the Google Recaptcha code that’s causing it to fail on my hosting. I’ve run into this before and resolve it by putting in a CURL rather than using the file_get_contents call. The symptom is that Recaptcha is unable to connect when trying to verify the Captcha. It doesn’t error out, rather it fails silently and presents the registration page again.,Hi I am also having problems with the recaptcha. When a user logs by the affiate login he is redirected to standard wordpress login page. the error says: ,Here are the error entries in the hosting logs:,I am using recaptcha V3. I have installed latest affiliate manger plus latest addon “Affiliates Manager and Google reCAPTCHA Integration”

Here are the error entries in the hosting logs:

2019 - 05 - 15 23: 57: 37 Warning 50.39 .126 .19 1583 #0: *27822 FastCGI sent in stderr: "PHP message: PHP Warning: file_get_contents(): SSL: Handshake timed out in /var/www/vhosts/juicedflavors.com/staging.juicedflavors.com/wp-content/plugins/affiliates-manager-google-recaptcha-integration/lib/ReCaptcha/RequestMethod/Post.php on line 68PHP message: PHP Warni" while reading upstream				nginx error
2019-05-15 23:57:37	Warning	50.39.126.19		1583# 0: * 27822 FastCGI sent in stderr: "ng: file_get_contents(): Failed to enable crypto in /var/www/vhosts/juicedflavors.com/staging.juicedflavors.com/wp-content/plugins/affiliates-manager-google-recaptcha-integration/lib/ReCaptcha/RequestMethod/Post.php on line 68PHP message: PHP Warning: file_get_contents(https://www.google.com/recaptcha/api/siteverify): failed to open stream: operation failed in /var/www/vhosts/juicedflavors.com/staging.juicedflavors.com/wp-content/plugins/affiliates-manager-google-recaptcha-integration/lib/ReCaptcha/RequestMethod/Post.php on line 68PHP message: PHP Warning: file_get_contents(): SSL: Handshake timed out in /var/www/vhosts/juicedflavors.com/staging.juicedflavors.com/wp-content/plugins/affiliates-manager-google-recaptcha-integration/lib/ReCaptcha/RequestMethod/Post.php on line 68PHP message: PHP Warning: file_get_contents(): Failed to enable crypto in /var/www/vhosts/juicedflavors.com/staging.juicedflavors.com/wp-content/plugins/affiliates-manager-google-recaptcha-integration/lib/ReCaptcha/RequestMethod/Post.php on line 68PHP message: PHP Warning: file_get_contents(https://www.google.com/recaptcha/api/siteverify): failed to open stream: operation failed in /var/www/vhosts/juicedflavors.com/staging.juicedflavors.com/wp-content/plugins/affiliates-manager-google-recaptcha-integration/lib/ReCaptcha/RequestMethod/Post.php on line 68"
while reading upstream
22%

Test your wits and sharpen your skills. Take the Challenge »,You verify it on the backend using the API, i.e. you have the challenge and the response from the client, and then you send those to Google and it tells you whether or not they're correct.,Track users' IT needs, easily, and with only the features you need.,I would appreciate it if someone could explain what I need to do to verify the recaptcha response.

  if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) {
    $secret = 'your_secret_key';
    $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=' . $secret . '&response=' . $_POST['g-recaptcha-response']);
    $responseData = json_decode($verifyResponse);
    if($responseData->success) {
      //contact form submission code
      $resultmsg = "<b>Thank you. Your message has been sent.</b>";
    } else { // verification failed
      $resultmsg = '<b>reCAPTCHA verification failed, please try again.</b>';
    }
  } else { // 'g-recaptcha-response' empty or not set
    $resultmsg = '<b>Please go back and click on the reCAPTCHA box.</b>';
  }
load more v
60%

<script src="https://www.google.com/recaptcha/api.js?hl=<?php echo $lang; ?>" async defer></script>

<!-- simple form with recaptcha included -->
<form action="" method="POST">
   <input type="text" name="name" value="name" />
   <input type="text" name="email" value="email" />
   <textarea type="text" name="message">Message .....</textarea>
   <div class="g-recaptcha" data-sitekey="YOUR SITE KEY"></div>
   <input type="submit" name="submit" value="SUBMIT">
</form>
load more v

Other "undefined-undefined" queries related to "Recaptcha Not Verifying with file_get_contents"