2

I'm using the Google Recaptcha library (https://github.com/google/recaptcha) to confirm form submissions. This has been working fine for months. Suddenly I started noticing a lot of errors like:

ErrorException: file_get_contents(): Failed to enable crypto in /vendor/google/recaptcha/src/ReCaptcha/RequestMethod/Post.php:68

(called from \ReCaptcha\ReCaptcha::verify) This exception can be replicated intermittently with a simple request (outside the library):

file_get_contents('https://www.google.com/recaptcha/api/siteverify');

When it errors, it takes about 52 seconds to time out before displaying the error, otherwise it succeeds right away. No idea where this timeout value is coming from. Older posts have suggested that this error is related to SSL3 (it's not--obviously Google is not using it, it uses TLS 1.2). I've also tried using the latest cacerts.pem file (from curl), but the problem persists.

I also have no problem pulling that URL with curl (from PHP or cli).

The server is running PHP 7.1.3 on Ubuntu 14.04. I do not experience this issue on my local machine, running the same PHP on Ubuntu 16.10, so I'm wondering if that could somehow be related. I'm really at a loss here. I know I can fork the library and rewrite it to use curl instead of file_get_contents, but that seems like a work-around at best.

hackel
  • 1,181
  • 1
  • 14
  • 21
  • As of today I have exactly the same issue (albeit with PHP 5). Did you ever find a solution? – MrCarrot Oct 04 '17 at 15:42
  • @MrCarrot, try setting `default_socket_timeout=360` in your `php.ini` and see if that helps – Tarun Lalwani Oct 30 '17 at 19:01
  • what is your server's OS? is it possible that it was a network issue? and maybe this is related? https://stackoverflow.com/questions/26148701/file-get-contents-ssl-operation-failed-with-code-1-and-more?rq=1 – am05mhz Oct 31 '17 at 09:21
  • For start you can set a timeout and do some retries (3 for example): Setting the timeout properly without messing with ini values: ` array( 'timeout' => 1 ) ) ); file_get_contents("http://example.com/", 0, $ctx); ?>` – BJovke Oct 31 '17 at 13:10
  • Launch a tcpdump and record the network communication. It might be that you have a firewall interfering, some limit imposed by Google reached, etc. – BJovke Oct 31 '17 at 13:12
  • What's the line just before the one you've pasted here, in your error.log? – Milos Nov 03 '17 at 23:25

1 Answers1

0

I would reduce the timeout, since users can't wait 52 seconds, and just let it fail.

Sometimes things fail, it's not a big deal, as long as you catch the errors and present the user with "soft" errors.

The problem is to keep the user waiting while the thing fails, that I think doesn't convey a great user experience.

Felipe Valdes
  • 1,998
  • 15
  • 26